Sign in to follow this  

Derived Func Automatically Calling Parent Func

This topic is 4384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys, I have a pattern that seems to repeat itself in my code : I find that i have a parent class which implements a virtual function. I then have a derived class which overrides the function, because it adds functionality, but i want the parent class's function to still be called. I know i can use ParentClass::VirtualFunction, but is there a way for the function to automatically be called so i dont have to remember add the ParentClass::VirtualFunction every time it's needed? (im wondering if there is a design pattern that is used in this scenario). Thanks AlexS

Share this post


Link to post
Share on other sites
no, because how would it know if it should call the parent BEFORE the child, AFTER the child, or in the MIDDLE of some functionality in the child ...

Hence the reason you have to write the call if you write an overload.

The only functions which auto call are Contructors (parent called before) and destructors (parent called after) ... because the language dictates the order rules for those functions.

Share this post


Link to post
Share on other sites
You could make a protected virtual function that is called by a public non virtual function that does some extra work:

class Base {
public:
void doStuff();
protected:
virtual void doMoreStuff();
};

void Base::doStuff() {
// base class stuff before
doMoreStuff();
// base class stuff after
}
void Base::doMoreStuff() { /* can be overriden */ }


class Derived : public Base {
protected:
virtual void doMoreStuff();
};

void Derived::doMoreStuff() {
// derived stuff here
}

Share this post


Link to post
Share on other sites
Nope. It's entirely up to the derived class to decide whether or not to call the base's method. You may with to do some pre-processing then call the base method, or you may wish to call the base method first, then do some post-procesing, or you may wish to call the base class somewhere in the middle, doing both pre- and post- processing.

In fact, you can even skip the base class call, and call the base class of the base class, skipping over the one in the middle, which does have it's uses also.

In Delphi you just write 'inherrited' (which is nicer), and it calls the immediate parent, though I think you can specify a different base class to call too, iirc.

Share this post


Link to post
Share on other sites

#define REGISTER_PARENT_CLASS(ParentClassName) typedef ParentClassName Super;






Then, in all your classes, simply do:


class Foo : public Boo
{
REGISTER_PARENT_CLASS(Boo);
}






Then you can do Super::Function() anytime, anywhere. Doesn't exactly solve your problem, but it makes it easier, especially when you add an intermediate class between Foo and Boo, you don't have to update all your code.

=[dEkS]=

Share this post


Link to post
Share on other sites

This topic is 4384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this