类函数/变量必须在使用之前声明吗? [英] Do class functions/variables have to be declared before being used?

查看:153
本文介绍了类函数/变量必须在使用之前声明吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在学习课程,我偶然发现了一些我觉得对我来说很尴尬的东西。

So I was learning about classes and I stumbled upon something I found was quite awkward to me.

class Nebla 
{
    public:
        int test()
        {
            printout();
            return x;
        }

        void printout()
        {
            printout2();
        }

    private:
        int x,y;
        void printout2()
        {
            cout<<"Testing my class";
        }
};

我发现在一个类中,我可以在声明它们之前使用函数>

I found that in a class I can use functions before I declare them (prototype them)


您可以看到我使用 printtout()

我可以在声明之前使用变量

And I can use variables also before declaring them


你可以看到我 return x ;在声明x之前。

为什么我可以在声明之前在类中使用函数和变量,但是如果我这样做,错误?

Why can I use functions and variables in classes before declaration but outside the class if I do that, I get an error?

感谢

推荐答案

我已经依赖这个功能多年没有考虑它。我浏览了几本C ++书籍,找到一个答案,包括Stroustrup的 C ++编程语言注释的C ++参考手册,但没有一个承认或解释的区别。

Good question; I've relied on that feature for years without thinking about it. I looked through several C++ books to find an answer, including Stroustrup's The C++ Programming Language and The Annotated C++ Reference Manual, but none acknowledge or explain the difference. But, I think I can reason through it.

我相信你的例子的原理是你的 test code>和 printtout 并不是真正出现在文件中的位置。代码

The reason, I believe, that your example works is that the bodies of your test and printout aren't truly where they appear in your file. The code

class MyClass {
  void someFun() {
    x = 5;
  }
  int x;
};

...这似乎违反了在使用变量之前声明变量的规则相当于:

...which appears to violate the rule of having to declare variables before you use them, is actually equivalent to:

class MyClass {
  void someFun();
  int x;
};

void MyClass::someFun() {
  x = 5;
}

一旦我们重写它, code> MyClass 定义实际上是一个声明的列表。这些可以是任何顺序。您在声明之前不依赖 x 。我知道这是真的,因为如果你重写这样的例子,

Once we rewrite it like that, it becomes apparent that the stuff inside your MyClass definition is actually a list of declarations. And those can be in any order. You're not relying on x until after it's been declared. I know this to be true because if you were to rewrite the example like so,

void MyClass::someFun() {
  x = 5;
}

class MyClass {
  void someFun();
  int x;
};

...它不会再编译!因此,类定义首先(包含成员的完整列表),然后您的方法可以使用任何成员,而不考虑它们在类中声明的顺序。

...it would no longer compile! So the class definition comes first (with its complete list of members), and then your methods can use any member without regard for the order in which they're declared in the class.

最后一个难题是C ++禁止声明类定义之外的任何类成员,所以一旦编译器处理你的类定义,它就知道类成员的完整列表。这在Stroustrup的Annotated C ++ Reference Manual的第170页中说明:成员列表定义类的所有成员,没有成员可以在其他地方添加。

The last piece of the puzzle is that C++ prohibits declaring any class member outside of the class definition, so once the compiler processes your class definition, it knows the full list of class members. This is stated on p.170 of Stroustrup's The Annotated C++ Reference Manual: "The member list defines the full set of members of the class. No member can be added elsewhere."

感谢您让我调查此事;我今天学到了新的东西。 :)

Thanks for making me investigate this; I learned something new today. :)

这篇关于类函数/变量必须在使用之前声明吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆