C ++: how to call a parent class function from outside

I have:

class A{ public: virtual void foo(); }; class B : public A{ public: void foo(); }; B *ptr = new B(); 

I want to call A foo() DIRECTLY using the 'ptr' pointer.

When i try

 (A*)ptr->foo(); 

it still calls version B of foo() . How can I name the version instead?

Is it possible? What are the alternatives? Thanks.

+8
c ++ inheritance pointers virtual
source share
2 answers

When you call a function a form form :: with resolution, you call the named function as if it were not virtual.

 ptr->A::foo(); 
+18
source share

You need to publish your features. You do this simply by making the following change:

 class A{ public: virtual void foo(); }; class B : public A{ public: void foo(); }; 

When you do not, the functions are automatically closed and inaccessible from the "outside".

+2
source share

All Articles