码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-16 00:29:25      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/xiumukediao/p/4649706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!