Home > database >  Why call a Base Class virtual member from an Inherited class?
Why call a Base Class virtual member from an Inherited class?

Time:09-18

I've done C stuff for a number of years, no formal training, though. Most of my background is aerospace where we had to always reinvent the wheel, no use of 3rd party libraries or APIs, so those are kind of new to me.

I'm working on something that does have an API and I am a little confused by some existing code. When asking co-workers, they don't have any answers, either.

I have a class that inherits the API's Application class. This Application class has a protected virtual function called onUpdate(), described as:

Function Callback for updating logic, called every frame

class MyApp : public Application {
public:
   virtual void onUpdate() override;
}
void MyApp::onUpdate() {
   Application::onUpdate(); // Why do this?
...
}

First off, I thought that callbacks would have some sort of function pointer, lambda, something in them.

The only answer that someone mentioned was that this was a "typical" thing to do so that the Application::onUpdate() function, which might be different, would be called. But doesn't declaring a virtual function override the base class implementation if there is one?

I would have thought that if this is necessary, the documentation would say something like "be sure to call the base class onUpdate function if you choose to write your own".

I've tried a break point at the call, it appears to do nothing. I'm wondering if the compiler is optimizing it out, even thought I have -O0. I can remove it and the code appears to run just fine, but it is quite complex, so I don't want to create some downstream issues.

I'm not even sure what to search on. I can't seem to find a similar example. Is there a formal name for this implementation? I'm having trouble wrapping my brain around this one.

CodePudding user response:

I think that original question has been answered as best as it can be given the info that I provided.

The implementation of the framework dictates how this is used. In my case, the specific framework is hiding a lot of details. While not common, this is not unheard of and can be considered an anti-pattern, "Call Super"

Thanks for the help.

CodePudding user response:

Why call a Base Class virtual member from an Inherited class?

If you want/need the overriding function to do what the base function does, then it can be useful to call that function instead of duplicating the code. It may even be necessary if that function accesses private state of the base.

I would have thought that if this is necessary, the documentation would say something like "be sure to call the base class onUpdate function if you choose to write your own".

In cases where it is necessary, the base class documentation should ideally say so.

Is there a formal name for this implementation?

If you mean "is there a name for this design pattern", there isn't enough details in your question to answer accurately, but based on the names alone, I would guess that this is part of the Observer pattern.

  • Related