c**********e 发帖数: 2007 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q98: Call member function in virtual function
发信站: BBS 未名空间站 (Fri Oct 21 20:04:02 2011, 美东)
What is the output of the following code? Why?
#include
using namespace std;
class base {
public:
void pay() { cout << "Base::pay" << endl; }
virtual void eat() { pay(); }
};
class derived: public base {
public:
void pay() { cout << "Derived::pay" << endl; }
};
void main() {
base* p = new derived;
p->eat();
} | d*l 发帖数: 1810 | 2 Base::pay
pay()并不是虚函数,调用的时候是直接绑定,既然是在Base::eat()中调用的,那么直
接绑定的结果就是Base::pay()被调用了
【在 c**********e 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: careerchange (Stupid), 信区: JobHunting : 标 题: C++ Q98: Call member function in virtual function : 发信站: BBS 未名空间站 (Fri Oct 21 20:04:02 2011, 美东) : What is the output of the following code? Why? : #include : using namespace std; : class base { : public: : void pay() { cout << "Base::pay" << endl; }
| b*****n 发帖数: 482 | 3 vtable is queried for eat(), not pay()
【在 c**********e 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: careerchange (Stupid), 信区: JobHunting : 标 题: C++ Q98: Call member function in virtual function : 发信站: BBS 未名空间站 (Fri Oct 21 20:04:02 2011, 美东) : What is the output of the following code? Why? : #include : using namespace std; : class base { : public: : void pay() { cout << "Base::pay" << endl; }
|
|