Sign in to follow this  

inheritance

This topic is 4546 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, say I have these two classes:
class MyClass
{
  virtual DoSomething() {//code}
}

class MyDerivedClass: public MyClass
{
  DoSomething(){//additional code}
}

Would it be possible to let the MyDerivedClass::DoSomething() member to (automatically) include the code of MyClass::DoSomething() (e.g. like the "inherited" keyword in delphi)? Or should I just copy the code in my editor?

Share this post


Link to post
Share on other sites
you can still use the functions available in the inherited class, meaning that you can call the inherited "DoSomething", from your own "DoSomething".

Share this post


Link to post
Share on other sites
Well it's more than just one keyword, but...

class MyClass
{
virtual DoSomething() {
//code
}
}

class MyDerivedClass: public MyClass
{
DoSomething() {
MyClass::DoSomething();
//additional code
}
}

Share this post


Link to post
Share on other sites
Not really, but if you really want to do something like that, then it's likely that a different idiom would work out:

class MyClass {
public:
void DoSomething(void) { // not virtual
// code
DoSomethingImpl();
}
private:
virtual void DoSomethingImpl() {} // potentially pure virtual
};

class MyDerivedClass : public MyClass {
private:
virtual void DoSomethingImpl() {
// derived class' behaviour
}
};

Share this post


Link to post
Share on other sites
constructors/destructors are automatically called when inherited, using "super" you can call them with custom arguments if necesarry.

REMEMBER to make the destructor virtual if you want to be safe.

Share this post


Link to post
Share on other sites

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