我想知道为什么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.
为什么这有效,而且大多数情况下,为什么只能在课堂上工作?