0

First of all apologies if this is a newbie question but I'm beginner at this. My question is a variant of the diamond problem I believe.

Imagine I have the following classes:

class A{ public: virtual foo();};

class B{ public: foo();};

class C{ public: foo();};

class D: public A, public B, public C{
    public: foo();
};

My question is is it possible to call foo() from class D and have the virtual function run across A, B, C and D. I have tried this but the d::foo() function overrides all the other foo() functions.

Or is it only possible if you make a 1:2 inheritance scheme?

class A{ public: virtual foo();};

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

class C: public B { public: foo();};

class D: public C { public: foo();};

The reason I ask is because I would like to be able to construct A, B and C in different classes without having to construct each time also A or B or C but retain the possibility of running the virtual function without having to call for each case A::foo(), B::foo(), C::foo().

gsamaras
  • 71,951
  • 46
  • 188
  • 305
hadroque
  • 15
  • 4
  • "My question is is it possible to call foo() from class D and have the virtual function run across A, B, C and D" What do you mean? – Brian Bi Jun 06 '14 at 18:38
  • Are you asking if you can call D.foo() and have it call A.foo(), B.foo() and C.foo() all at once? – bstar55 Jun 06 '14 at 18:41
  • Yes bstar55. I would like to call all of them at the same time. So calling D.foo() would call A.foo(), B.foo(), C.foo() and D.foo(). changed the question to try and be less confused. – hadroque Jun 06 '14 at 18:51

2 Answers2

0

You'll have to have to implement class D::foo explicitly call A::foo(), B::foo(), and C::foo(). There's no way to make it automatically call foo() on each parent class.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
user1483946
  • 151
  • 2
0

This is called function overloading, so you can not do what you want automatically.

You have to do it like this:

#include <iostream>

class A {
 public:
  virtual void foo() {
    std::cout << "foo of A\n";
  }
};

class B {
 public:
  // virtual keyword is not needed,
  // but it's to write it so that you
  // remind the reader that foo is virtual
  virtual void foo() {
    std::cout << "foo of B\n";
  }
};

class C {
 public:
  virtual void foo() {
    std::cout << "foo of C\n";
  }
};

class D : public A, public B, public C {
 public:
  virtual void foo() {
    A::foo();
    B::foo();
    C::foo();
    std::cout << "foo of D\n";
  }
};

int main()
{
  D d_object;
  d_object.foo();
  return 0;
}

However, I strongly suggest you to read this answer, since you probably receive:

has virtual method..but not virtual constructor

Usually warnings are wise to be treated like real errors, since they usually expose logical errors.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thanks for the pointers. This example could also be constructed without declaring a virtual function if I understand this correctly? Just by overloading the function foo(). – hadroque Jun 06 '14 at 19:22
  • @hadroque true. But you could just remove the `virtual` keyword and find out. I hope you understand now why this happening. You are welcome. – gsamaras Jun 06 '14 at 19:25
  • By the way I think the -1 in your question is too much for a beginner, so I am voting up for balancing @hadroque . :) – gsamaras Jun 06 '14 at 19:31