Jump to content
  • Advertisement
Sign in to follow this  
tjrr3d

c++ inheritance question

This topic is 4415 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

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
Advertisement
You should of course ensure that the parent class has a virtual destructor, and also ensure that any member functions you redefine are virtual too.

Share this post


Link to post
Share on other sites
is it a rule of thumb that all inherited classes have their respective destructors virtualized? even if they are non-existant or empty?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!