c++各个对象之间要相互调用,从A对象调B对象的成员函数,方法是forward declaration,如下面的例子:
//file1.h declaration of A
class A
{
public:
A();
~A();
void fun();
........
private:
........
};
//file1.h end!
//file1.cpp definition of A
A::A()
{
.....
}
A::~A()
{
.....
}
A::fun()
{
........
}
//file1.cpp end!
要在class B 里调用A;则B可以如此
//file2.h declaration of B
class A;
class B
{
public:
B();
~B();
// void function(){a->fun();};
......
private:
A* a;
........
}
//file2.h end!
//file2.cpp
B::B()
{......}
B::~B()
{......}
.........
//file2.cpp end
注意: class B 声明里面" // void function(){a->fun();};"前面的注释"//"不能去掉,否则会有error: forward declaration,编译通不过,也就是不能有对B对象的操作