Sign in to follow this  
tjrr3d

c++ inheritance question

Recommended Posts

Say I inherit a class but instead of overriding a function, I want to append on to it. Is there a way to call the parent function first from the child class? example:
class Parent
{
    public:
       Parent();
       ~Parent();

       Update();
};

Parent::Parent(){
}
Parent::~Parent(){
}
Parent::Update(){
  // Update X
}



class Child : public Parent
{
    public:
       Child();
       ~Child();

       Update();
};

Child::Child(){
}
Child::~Child(){
}
Child::Update(){
  // Call parent Update() to update X
  // Now update y
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by pcxmac
is it a rule of thumb that all inherited classes have their respective destructors virtualized? even if they are non-existant or empty?
With public inheritance yes (99.5% of cases). With private and protected inheritance it depends.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by tjrr3d
Say I inherit a class but instead of overriding a function, I want to append on to it. Is there a way to call the parent function first from the child class?

example:
*** Source Snippet Removed ***
As an alternative to what hplus0603 said, a "template method" may be used:


class Parent
{
public:
Parent();
virtual ~Parent();

void Update() { ...code...; doUpdate(); }
private:
virtual void doUpdate()=0;
};


And now Child-class just needs to do whatever it wants in doUpdate, and not worry about what else the parent-class's Update is doing. When using the class, you'll actually be calling Update (a public method) instead of doUpdate (a virtual private method).

Share this post


Link to post
Share on other sites

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