您的位置:首页 > 其它

解决两类相互包含使用另一个类成员函数

2015-07-15 22:27 274 查看
// VistorMode.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <IOSTREAM>
using namespace std;

class B;
class A{
public:
/*
void getBaction(B * p){
p->action();         //会出错,error:use of undefined type 'B'
//因为前面只声明了这个类,但类的成员函数还未知,只能定义一个B类型的指针
}
*/

void getBaction(B * p);  //实现放于B的action声明定义后面
void action(){
cout<<"action in A"<<endl;
}
};

class B{
public:
void getAaction(A * p){
p->action();
}
void action(){
cout<<"action in B"<<endl;
}
};
void A::getBaction(B * p){
p->action();
}
int main(int argc,char * argv[]){
A a ;
B b ;
a.getBaction(&b);
b.getAaction(&a);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: