Robin Riddarstjerna
Robin Riddarstjerna

Reputation: 17

Multiple and virtual inheritance C++

Full disclaimer, this is homework - not graded, just given to students so we can practice. I'm asking for help, because we won't get an answer and I just want to know how to solve it.

What I can do is define structures B and C. Their interface has to be "like A's interface, with modifications so it works correctly". I can't add any new methods. I also can't change anything in struct A.

This is the code:

#include <iostream>

struct A;
struct B;
struct C;

struct A {
    A() {
        std::cout << __PRETTY_FUNCTION__ << "\n";
    }
};

int main(){
    C c;
}

And the desired output is:

A::A()
A::A()
B::B()
A::A()
A::A()
B::B()
A::A()
C::C()

What I tried to do so far: First I started like this, just to check things out:

struct B : public A {
  B() : A() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

struct C : public B {
  C() : B() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

Which gave me:

A::A()
B::B()
C::C()

So I tried to get first C, then A, then B, then A (desired output from the bottom):

struct B : public virtual A {
  B() : A() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

struct C : public B, public A {
  C() : A(), B() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

But of course this didn't work. (warning: direct base ‘A’ inaccessible in ‘C’ due to ambiguity)

Adding virtual keyword like below gave me again C, B then A, from the bottom:

struct B : public virtual A {
  B() : A() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

struct C : public virtual A, public B {
  C() : A(), B() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

If I want to get C, then A I have to do the following (but then there will be no B)

struct C : public virtual A {
  C() : A() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

struct B : public virtual A, public C {
  B() : C(), A() {
      std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

I also have no idea how could I get A::A() twice in a row.

I just want to understand how this should work, but if you still feel like you don't want to help me with a solution, then please leave me some tips.

Upvotes: 0

Views: 131

Answers (2)

anastaciu
anastaciu

Reputation: 23792

You can do something like:

struct B : A { //inherit from A
    B() {
        A();
        std::cout << __PRETTY_FUNCTION__ << "\n";       
    }
};

struct C : B { //inherit from B
    C() {
        B();   //anonymous B object
        A();   //anonymous A object
        std::cout << __PRETTY_FUNCTION__ << "\n";
    }
};

Running sample

Upvotes: 1

a1ezh
a1ezh

Reputation: 422

I do not see a restriction in the exercise on use of class members:

struct A
{
    A() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};

struct B : A
{
    B() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};

struct C
    : B
    , A
{
    C() { std::cout << __PRETTY_FUNCTION__ << "\n"; }

    B b;
    A a;
};

UPDATE

The desired output had changed since my first answer was posted. So previous answer had become wrong. But now you see the point - you can use composition and copy A a; to B definition and remove A inheritance from C definition.

Upvotes: 2

Related Questions