ABCplus
ABCplus

Reputation: 4021

Constructors inheritance vs methods inheritance in C++ objects

Is there a way under C++ to have the object methods inheritance acting like constructors inheritance? Let's have the following:

(myclass.h)

class B{
public:
    B();

    void doSomething();
};

class A : public B{
public:
    A();

    void doSomething();
}

(myclass.cpp)

B::B()
{
    executeBCode();
}

void B::doSomething()
{
    executeBCode();
}

A::A : B()
{
    executeACode();
}

void A::doSomething()
{
    executeACode();
}

What happens is that calling the A constructor, I execute the executeBCode() and then the executeACode(). With method call doSomething() on A object I execute just the executeACode(), because it's overridden. Is there a way to have inheritance on methods behave like inheritance on constructors, so executing both the derived and base code? I came into this question because I need that the base class in my code does something for its derived class, and moreover the derived should execute its own instructions: this simply calling the doSomething() function on the derived. This could be the case of a closeAll() method called on A for example, where the base class close its own base objects and the derived its specific stuff.

Upvotes: 2

Views: 86

Answers (3)

Niall
Niall

Reputation: 30605

If I understand your intent here, it is not possible to do this directly in the language (it may be in other languages). There are two alternative mechanisms/techniques that can be used to achieve a similar result.

A call to the base function from the derived class's function would work;

void A::doSomething()
{
    B::doSomething();
    executeACode();
}

Using the template method pattern (as used in the C++ IO streams library), also known as the NVI - non virtual interface, would also work;

class B {
    void executeBCode();
    virtual void doSomethingImp() = 0; // or not pure
public:
    void pubDoSomething() { // public facing
        executeBCode(); // order these as required
        doSomethingImp();
    }
};

class A {
    void executeACode();
    void doSomethingImp() override // internal implementation
    {
        executeACode();
    }
};

Upvotes: 1

JBL
JBL

Reputation: 12907

It's not possible directly, however you can achieve something close enough by using the Non-virtual interface idiom the following way:

class A {
public:
    void someFunction() {
        //put the base's code here
        someFuncImpl();//Calls the private function here
        //or here, as you see fit
    }        
private:
    virtual void someFuncImpl();//potentially pure: = 0;
};

and then in another class:

class B : public A {
private:
    virtual void someFuncImpl() {
        //Derived's code goes here
    }
};

That way, when you call someFunction() on class B, you'll have both the code of the derived class and the code of the base class executed each time.

That's not as simple as the way constructors can do it, but I think that's the closest and simplest you'll get. You get as a bonus the choice to run the base's code before or after the derived's code.

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258618

There's no built-in mechanism outside of constructors and destructors.

Closest you can get is calling the base methods yourself, or using the template method pattern.

class B{
public:
    B();
    void doSomething();
};

class A : public B{
public:
    A();
    void doSomething()
    {
       B::doSomething();
    }
}

Upvotes: 0

Related Questions