c**********e 发帖数: 2007 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q97: reference and virtual
发信站: BBS 未名空间站 (Fri Oct 21 19:58:51 2011, 美东)
What is the output of the following code? Why?
#include
using namespace std;
class student {
public:
virtual void sleep() { cerr << "student sleep" << endl; }
};
class grad: public student {
public:
virtual void sleep() { cerr << "grad sleep" << endl; }
};
void exam(student& stu) {
stu.sleep();
}
void main() {
grad g;
exam(g);
} | d*l 发帖数: 1810 | 2 glad sleep
引用传参实际效果和指针一样,对象仍然是完整的派生类对象
【在 c**********e 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: careerchange (Stupid), 信区: JobHunting : 标 题: C++ Q97: reference and virtual : 发信站: BBS 未名空间站 (Fri Oct 21 19:58:51 2011, 美东) : What is the output of the following code? Why? : #include : using namespace std; : class student { : public: : virtual void sleep() { cerr << "student sleep" << endl; }
|
|