c**********e 发帖数: 2007 | 1 #include
using namespace std;
class A {
public:
A() { cout<<"A_constructor "; f(); }
virtual void f() { cout<<"A_f "; }
};
class B: public A {
public:
B() { cout<<"B_constructor "; f(); }
virtual void f() { cout<<"B_f"; }
};
int main()
{ B b; }
What is the output?
a) A_constructor A_f B_constructor A_f
b) A_constructor A_f B_constructor B_f
c) Cannot call f() in A's constructor
d) None of above. |
d********t 发帖数: 9628 | |
q**r 发帖数: 611 | 3 怎么derived class也用virtual啊? 隐约记得constructor不能用virtual的, 估计
又记错了 |
c**********e 发帖数: 2007 | 4 The word virtual in derived class does not matter. The derived class
function is virtual if the base class member function is virtual.
【在 q**r 的大作中提到】 : 怎么derived class也用virtual啊? 隐约记得constructor不能用virtual的, 估计 : 又记错了
|
c**********e 发帖数: 2007 | 5 The question is pretty interesting, because the object is not constructed
yet when the member function is called.
Therefore, there is a question which f() the call f() in the constructor of
derived class call? |