Alex Rivera | Logout

Is there a way to call a base function outside of the class in C++?

Asked 2013-01-11T23:59:06.707
21

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.

Edit
Report

1 Answer

0

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

class Base
{
public:
    virtual bool Function();
    virtual bool OtherFunction();
};

typedef bool (Base::*)() BaseFunc;

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.

answered 2013-01-12T04:27:40.597

Your Answer