|
|||||
|
|
#1 |
|
|
{ public: method_A() { cout << "A method " << endl; } }; cl*** B : public A { public: method_B() { cout << "B method " << endl; } }; int main() { A* aptr = new B; .... } Why cann't "aptr" see method_B() of cl*** B since "aptr" is a pointer pointing actually at an object of type B? Thanks in advance! |
|
|
#2 |
|
|
> cl*** A > { > public: > method_A() { cout << "A method " << endl; } You mean void method_A() { ... > }; > > cl*** B : public A > { > public: > method_B() { cout << "B method " << endl; } You mean void method_B() { ... > }; > > int main() > { > A* aptr = new B; > ... > } > > Why cann't "aptr" see method_B() of cl*** B since "aptr" is a pointer > pointing actually at an object of type B? Because the _static_ type of aptr is A*. And it is not "pointing actually" at a B, it is pointing to an A inside the B. If you want to get to 'method_B', you will have to static_cast your 'aptr' to 'B*': static_cast<B*>(aptr)->method_B(); Victor |