类函数/变量是否必须在使用前声明?

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

问题描述

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

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)

你可以看到我在声明之前使用了 printout() , printout2() .

You can see I used printout() , printout2() before decleration.

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

And I can use variables also before declaring them

你可以看到我做了return x;在声明 x 之前.

You can see I did return x; before declareing 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.

我相信,您的示例之所以有效,是因为您的 testprintout 的正文并未真正出现在您的文件中.代码

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;
}

一旦我们像这样重写它,很明显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 的带注释的 C++ 参考手册的第 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. :)

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

上一篇:为“make_function"推断 lambda 或任意可调用的调 下一篇:C++ 从多个具有相同虚函数名称的基类继承

相关文章

最新文章