Sign in to follow this  
Kalasjniekof

inheritance

Recommended Posts

Kalasjniekof    246
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
Syranide    375
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
SiCrane    11839
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
SiCrane    11839
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
Syranide    375
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

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