只要通过"引用"你的意思是指针,这应该工作:
class Foo;
class Bar {
Foo* p;
};
class Foo {
Bar* p;
};
Run Code Online (Sandbox Code Playgroud)
您可以转发声明类,然后在文件中定义它们:
class A;
class B
{
// As pointed out by syam this will have to be an A* or A& not just of type A.
// If this line were:
// A myA
// The compiler gives error: field ‘myA’ has incomplete type
A* myA;
};
class A {};
Run Code Online (Sandbox Code Playgroud)
如果您想在任何时候A从方法中访问方法或属性,B那么您必须确保在定义之后定义这些方法A.
class A;
class B
{
A& myA;
int getAValue(void); // Can't use myA.value here as value is not declared yet.
};
class A
{
public:
int value;
};
int B::getAValue(void) {return myA.value;}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |