相关疑难解决方法(0)

为什么在课堂内不需要"使用前声明"规则?

我想知道为什么C++的declare-before-use规则不能保存在类中.

看看这个例子:

#ifdef BASE
struct Base {
#endif
    struct B;
    struct A {
        B *b;
        A(){ b->foo(); }
    };

    struct B {
        void foo() {}
    };
#ifdef BASE
};
#endif

int main( ) { return 0; }
Run Code Online (Sandbox Code Playgroud)

如果定义了BASE,则代码有效.

在A的构造函数中,我可以使用尚未声明的B :: foo.

为什么这有效,而且大多数情况下,为什么只能课堂上工作?

c++ class forward-declaration

12
推荐指数
2
解决办法
3163
查看次数

标签 统计

c++ ×1

class ×1

forward-declaration ×1