19

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.

Community
  • 1
  • 1
user
  • 7,123
  • 7
  • 48
  • 90
  • 1
    `static_cast` might work, although I'm not sure - the point of virtual functions is that they are resolved at runtime, independently of the object's compile time type.. `dynamic_cast` will certainly not work. – lethal-guitar Jan 12 '13 at 00:08
  • Why would you want to? If function is part of the derived class and also in base-class, then you are probably doing something wrong when you call the base-class. The better way to solve that is to make a separate function in the base-class that is meant to be called by the derived class, and let the base-class call that functoion itself. – Mats Petersson Jan 12 '13 at 00:12
  • Please see comment to @Omnifarious below. – user Jan 12 '13 at 00:31

3 Answers3

27

Try derived_object.Base::function();

David G
  • 94,763
  • 41
  • 167
  • 253
Eric
  • 393
  • 2
  • 5
  • 1
    @Oliver: Yes it is standard. – Mankarse Jan 12 '13 at 00:11
  • 5
    @Oliver class::function() is the 'full name' of a member function. If you don't specify the class for the function you called, it would be the 'default'(derived one in your case) one. – Eric Jan 12 '13 at 00:19
12

I believe the syntax:

derived_ptr->Base::function();

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.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • 1
    Thanks-- I have an Base object that makes a report, and the Derived object that generates a report (with extra information over the base report). So I create the base report via Base::generate_report and then copy in the extra information. This is done outside the class using a visitor pattern (better separation of concerns)-- BaseReport is constructed with a Base and DerivedReport is constructed with a Derived. Phew! – user Jan 12 '13 at 00:08
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.

Zack Yezek
  • 1,408
  • 20
  • 7