您的位置:首页 > 其它

CHAPTER 05:EX 04

2008-05-18 00:02 369 查看
/*
4. Write two classes, each of which has a member function
that takes a pointer to an object of the other class. Create
instances of both objects in main( ) and call the
aforementioned member function in each class.
*/

#include<iostream>
using namespace std;

//如果要让一个类里面的成员函数的参数是另一个类,那么必须要先申明另外一个类
//这样的声明让编译器先看到需要用到的类,以便完成非完全类型检验。
class B;

class A {
public:
//获取B类型对象,并返回那个B类型对象的地址
B* addrB(B*);
};

class B {
public:
//获取A类型对象,并返回那个A类型对象的地址
A* addrA(A*);
};

B* A::addrB(B* b) {
return b;
}

A* B::addrA(A* a) {
return a;
}

int main() {
B b;
A a;
cout << "B class b object's address: "
<< &b << endl;
cout << "A class a object's member function's result: "
<< a.addrB (&b) << endl;
cout << "A class a object's address: "
<< &a << endl;
cout << "B class b object's member function's result: "
<< b.addrA (&a) << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: