// 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; }
时间: 2024-10-27 12:38:44