This question is similar, but is about calling the function from inside the class: Can I call a base class's virtual function if I'm overriding it?
In that case, you'd specify Base::function()
instead of function()
, which will call the overridden definition.
But is there a way to do this outside of the class? My class doesn't define a copy constructor, so I couldn't figure out how to cast as the base class:
Base( derived_object ).function()
Is the appropriate thing to do here to cast & derived_object
as Base*
and then call ->function()
?
Thanks for your insight.
I believe the syntax:
works just fine. Though I really question why you would want to do this in a function that's not part of your class. Especially if
function
happens to be a virtual function.The reason why it's a questionable idea is that you're making whatever it is that uses that notation depend on the inheritance hierarchy of your class. Also, functions are usually overridden for a reason. And you're getting around that by using this syntax.
Try
derived_object.Base::function();
You probably want to use pointers to member functions of a class. Those give you the ability to map different base class functions to the pointer as needed, and the ability to use it as a variable or function parameter.
The syntax for a pointer to a member function looks like
The list of caveats for using these things is a mile long- there is plenty online on how to use them (most of the answers are "don't"). However, they do give you a way of clearly binding to and calling a base class member function.