inheritance

Started by
4 comments, last by Syranide 18 years, 9 months ago
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?
Advertisement
you can still use the functions available in the inherited class, meaning that you can call the inherited "DoSomething", from your own "DoSomething".


Well it's more than just one keyword, but...
class MyClass{  virtual DoSomething() {    //code  }}class MyDerivedClass: public MyClass{  DoSomething() {    MyClass::DoSomething();    //additional code  }}
Ok, that works, but can it be done automatically like with constructors/destructors?
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    }};
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.


This topic is closed to new replies.

Advertisement