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

// 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;
}

 

posted @ 2015-07-15 22:27  朽木可雕否  阅读(221)  评论(0编辑  收藏  举报