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

Do class functions/variables have to be declared before being used?(类函数/变量是否必须在使用前声明?)
本文介绍了类函数/变量是否必须在使用前声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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. :)

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why does C++ compilation take so long?(为什么 C++ 编译需要这么长时间?)
Why is my program slow when looping over exactly 8192 elements?(为什么我的程序在循环 8192 个元素时很慢?)
C++ performance challenge: integer to std::string conversion(C++ 性能挑战:整数到 std::string 的转换)
Fast textfile reading in c++(在 C++ 中快速读取文本文件)
Is it better to use std::memcpy() or std::copy() in terms to performance?(就性能而言,使用 std::memcpy() 或 std::copy() 更好吗?)
Does the C++ standard mandate poor performance for iostreams, or am I just dealing with a poor implementation?(C++ 标准是否要求 iostreams 性能不佳,或者我只是在处理一个糟糕的实现?)