下面这段简单的代码可以编译,尽管我不明白为什么:
class C {
class B;
class A {
B getB() { return B(); }
};
class B {
};
};
int main(int, char**)
{
return 0;
}
如果我注释掉 "class C
" 部分,这样B
的前向声明、A
的定义和B
的定义就不再嵌套在类中了,代码将无法编译,因为B
是一个不完整的类型:
main.cpp: In member function 'B A::getB()':
main.cpp:6: error: return type 'struct B' is incomplete
main.cpp:6: error: invalid use of incomplete type 'struct B'
main.cpp:3: error: forward declaration of 'struct B'
我明白类型不完整的含义,即它尚未被定义,因此编译器无法知道要分配多少空间。但是,在上面的代码中为什么不认为B
是不完整的,其中A
和B
都在C
中声明和定义?
result
类,其中嵌套了tuple
和field
类,在其中tuple
在其一个函数体中构造了一个field
(即第183行)。 - villapx