您的位置:首页 > 编程语言 > C语言/C++

c++之this指针做参数实现对象间的交互实例

2014-04-26 23:43 555 查看
#include <iostream>

using namespace std;

class Student; //Student类声明

class Teacher{

public:

void educate(Student* ps);

void reply(const string& answer){

m_answer = answer;

}

private:

string m_answer;

};

class Student{

public:

void ask(const string& question, Teacher* pt){

cout << "问题:" << question << endl;

pt -> reply("不知道!");

}

};

inline void Teacher:: educate(Student* ps){

ps -> ask("什么是this指针?", this);

cout << "答案:" << m_answer << endl;

}

int main(void){

Teacher t;

Student s;

t.educate(&s);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐