前向应用声明
#include "stdafx.h"
#include <iostream>
using namespace std;
//两个类互相依赖,解决循环依赖的问题
class B;//前向引用声明,如不写下边就报错,写了就不错
class A {
public:
void f(B b);//这里会报错,编译错误,(就是这里)
};
class B {
public:
void g(A a);
};
前向引用声明不是万能的
#include "stdafx.h"
#include <iostream>
using namespace std;
//两个类互相依赖,解决循环依赖的问题
class Fred;//前向引用声明,写了下面还是报错
class Barney {
public:
Fred x;//这里会报错,编译错误,使用了未定义的class"Fred"(就是这里)
//因为Fred并没有给出完整的定义,所以Barney不能使用Fred中的数据成员
};
class Fred {
public:
Barney y;//因为Barney已经完整定义了,所以这里可以
};
int main()
{
return 0;
}