我想知道为什么 C++ 的使用前声明规则在类中不成立。
看这个例子:
#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; }
如果定义了 BASE,则代码有效。
在 A 的构造函数中,我可以使用尚未声明的 B::foo。
为什么这行得通,而且大多数情况下,为什么只在班级内有效?