I have below structure defied as
struct A{
string s;
A() {
cout<<"in a default\n";
}
A(string t): s(t) {
cout<<"in a param \n";
}
};
struct B: virtual public A{
B(): A("B"){
cout<<"in b\n";
}
};
struct C: virtual public A {};
struct D: public B, public C {};
int main()
{
D d;
}
with A as virtual base class in both C and B,output is as follows
in a default
in b
With A as virtual base class for only B ,output is as follows
in a default
in b
in a default
with A as virtual base class for only C ,output is as follows
in a default
in a param
in b
and with no virtual base class ,output is as follows
in a param
in b
in a default
can any one explain me the deviation?
Virtual inheritance is there to resolve what's known as the "Diamond of death"
In summary, the issue is that if you called D::[some function in A], which A would it pick - the one from B, or the one from C, or that makes no sense that the separation exists?
You can read more about the issue here.
https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem
Related
Consider the code
struct Base{};
struct Derived: public Base{};
struct A: public Base{};
struct B: public A, public Base{};
struct C: public A, public Derived{}; // why no ambiguity here?
int main() {}
The compiler (g++5.1) warns that
warning: direct base 'Base' inaccessible in 'B' due to ambiguity struct B: public A, public Base{};
I understand this, Base is duplicated in B.
Why is there no warning for C? Doesn't C inherit from both A and Derived, which both inherit from Base?
Why adding virtual
struct Derived: virtual Base{};
results now in both B and C emitting warnings, live on Wandbox
warning: direct base 'Base' inaccessible in 'B' due to ambiguity struct B: public A, public Base{};
warning: direct base 'Base' inaccessible in 'C' due to ambiguity struct C: public A, public Derived{};
In B, it's impossible to refer to members of the Base subobject inherited directly. Consider:
struct Base {
int x;
};
struct B: public A, public Base {
void foo() {
int& x1 = A::x; // OK
int& x2 = x; // ambiguous
// no way to refer to the x in the direct base
}
};
In C this is not a problem. Both x's can be referred to using qualified names:
struct C: public A, public Derived {
void foo() {
int& x1 = A::x; // OK
int& x2 = Derived::x; // OK
}
};
So the warning you get is one that only makes sense when a direct base is also inherited through another path.
For your second question, I couldn't reproduce the warning with C on Coliru with g++-5.1.
There are no ways to access unambiguously to Base members in "B" whereas it's possible in "C", as illustrated in the following code:
#include <iostream>
using namespace std;
struct Base
{
void print()
{
cout << "Base" << endl;
}
};
struct Derived : public Base {};
struct A : public Base
{
void print()
{
cout << "A" << endl;
}
};
struct B : public A, public Base
{
void print()
{
A::print();
//error (ambiguous), no way to access to Base::print => warning
//Base::print();
}
};
struct C : public A, public Derived
{
void print()
{
A::print();
Derived::print(); // Not Ambiguous, it's the Base inherited by 'Derived' which is used.
// Still an error but you can access print indirectly through "Derived" => no warning needed
//Base::print();
}
};
int main()
{
B b;
b.print();
C c;
c.print();
return 0;
}
Could I do this?
class A {
public:
virtual void aFoo() = 0;
};
class B : virtual public A {
public:
virtual void aFoo() { ... }
};
class D : public A {};
class C : public B, virtual public D {};
The issue is with the implementation of aFoo() in B and the lack of it in C. When it comes to compiling I see this error:
error: no unique final overrider for ‘virtual void A::aFoo()’ in ‘C’
Shouldn't it be possible to override a pure virtual method of a virtual base class in B?
Just edited the example to match the actual use case. Now looking at it in this simplified way I am not quite sure if this is even possible, let alone good design.
You need virtual inheritance in this way:
struct A {
virtual int f() = 0;
};
struct B : virtual A {
int f() { return 1; }
};
struct C : virtual A {};
struct D : B, C {};
int main() {
D d;
return d.f();
}
In the dupe I commented you can see this relation
A
/ \
B C
\ /
D
for virtual inheritance and
A A
| |
B C
\ /
D
without virtual inheritance.
In the second case D contains two function definitions. One is implemented and one isn't.
The compiler is complaining the constructor of D is deleted because of ill forming why ?
#include<iostream>
using namespace std;
class A
{
int x;
public:
A(int i) { x = i; }
void print() { cout << x; }
};
class B: virtual public A
{
public:
B():A(10) { }
};
class C: virtual public A
{
public:
C():A(10) { }
};
class D: public B, public C {
};
int main()
{
D d;
d.print();
return 0;
}
Output
main.cpp:37:4: error: use of deleted function 'D::D()' D d;
^ main.cpp:32:7: note: 'D::D()' is implicitly deleted because the default definition would be ill-formed: class D: public B, public C {
^
Due to the rules for initialization of virtual base classes,
class D: public B, public C {
};
is equivalent to:
class D: public B, public C {
public:
D() : A(), B(), C() {}
};
That's why you cannot create in instance of D.
Solution 1
Change A so it has a default constructor.
class A
{
int x;
public:
A(int i = 0) { x = i; }
void print() { cout << x; }
};
Solution 2
Change D to:
class D: public B, public C {
public:
D() : A(0), B(), C() {}
};
or a simpler version,
class D: public B, public C {
public:
D() : A(0) {}
};
That's because D inherits from A indirectly using virtual. A doesn't have a parameterless constructor so a compiler-generated constructor for D can't be made.
Note: this is mostly just adding a reference to the standard, in case anybody might care (but as usual for him, #R. Sahu's answer is quite accurate).
The standard specifies ([class.base.init]/13) that:
In a non-delegating constructor, initialization proceeds in the
following order:(13.1) — First, and only for the constructor of the
most derived class (6.6.2), virtual base classes are initialized in
the order they appear on a depth-first left-to-right traversal of the
directed acyclic graph of base classes, where “left-to-right” is the
order of appearance of the base classes in the derived class
base-specifier-list.(13.2) — Then, direct base classes are
initialized in declaration order as they appear in the
base-specifier-list (regardless of the order of the mem-initializers).
So, since A is a virtual base class, it's initialized directly by the most derived class (D). Only afterward, the direct base classes are initialized--but for anything to compile, the most derived class must be able to initialize the virtual base class(es).
There is one point some might find interesting in a case like this. Let's modify your class structure just a tiny bit, so we to the necessary initialization, and (importantly) initialize with a unique value in each constructor:
#include <iostream>
class A {
int i;
public:
A(int i) : i(i) {}
void show() { std::cout << "value: " << i << "\n"; }
};
class B : virtual public A{
public:
B() : A(10) {}
};
class C : virtual public A {
public:
C() : A(20) {}
};
class D : public B, public C {
public:
D() : A(0) {}
};
int main() {
D d;
d.show();
}
In this case, what exactly happens? We have three different constructors each "thinking" it's going to initialize the A object with a different value? Which one "wins"?
The answer is that the one in the most-derived constructor (D::D) is the one that' used to initialize the virtual base class object, so that's the one that "wins". When we run the code above, it should print 0.
class diagram
class A {public: virtual int func();};
class B: virtual public A {};
class C: virtual public A {};
class D: virtual public C {public: virtual int func();};
class E: public B, public D {};
// e is an object of E
e->func(); // this will run func defined in A, not in D
I have a multiple inheritance situation as in the above example. How can I implement this to call the most derived method? (To call func() implemented in D, not in A, when I write e->func())
If you have implemented func in D, that will be called. You can see the running example here
https://ideone.com/kwJubg
class A {public: virtual int func(){cout<<"In A";};};
class B: virtual public A {};
class C: virtual public A {};
class D: virtual public C {public: virtual int func(){cout<<"In D";};};
class E: public B, public D {};
int main()
{
E *e = new E();
e->func();
return 0;
}
When I test your example I get "D" not "A" as you mentioned.
class A {public: virtual void func() {std::cout << "A" << std::endl;} };
class B: virtual public A {};
class C: virtual public A {};
class D: virtual public C {public: virtual void func() {std::cout << "D" << std::endl;} };
class E: public B, public D {};
int main()
{
E e {};
e.func();
}
You have the "Diamond" problem.
A
B D // only B or D can override A::func()
E // can override A::func()
The tip of the Diamond is class A, and it has two classes that derive from it, "B" and "D", on each side of the diamond. Only one of the derived classes that are part of the sides of the diamond can override a virtual function from the tip of the diamond, not both of them (Note that the bottom of the diamond can override, in this case E). In this case is class D which overrides func. If class B would override func it wouldn't compile. Also if class "B" and "D" don't override anything, in your example, A::func() would be called.
Consider the code
struct Base{};
struct Derived: public Base{};
struct A: public Base{};
struct B: public A, public Base{};
struct C: public A, public Derived{}; // why no ambiguity here?
int main() {}
The compiler (g++5.1) warns that
warning: direct base 'Base' inaccessible in 'B' due to ambiguity struct B: public A, public Base{};
I understand this, Base is duplicated in B.
Why is there no warning for C? Doesn't C inherit from both A and Derived, which both inherit from Base?
Why adding virtual
struct Derived: virtual Base{};
results now in both B and C emitting warnings, live on Wandbox
warning: direct base 'Base' inaccessible in 'B' due to ambiguity struct B: public A, public Base{};
warning: direct base 'Base' inaccessible in 'C' due to ambiguity struct C: public A, public Derived{};
In B, it's impossible to refer to members of the Base subobject inherited directly. Consider:
struct Base {
int x;
};
struct B: public A, public Base {
void foo() {
int& x1 = A::x; // OK
int& x2 = x; // ambiguous
// no way to refer to the x in the direct base
}
};
In C this is not a problem. Both x's can be referred to using qualified names:
struct C: public A, public Derived {
void foo() {
int& x1 = A::x; // OK
int& x2 = Derived::x; // OK
}
};
So the warning you get is one that only makes sense when a direct base is also inherited through another path.
For your second question, I couldn't reproduce the warning with C on Coliru with g++-5.1.
There are no ways to access unambiguously to Base members in "B" whereas it's possible in "C", as illustrated in the following code:
#include <iostream>
using namespace std;
struct Base
{
void print()
{
cout << "Base" << endl;
}
};
struct Derived : public Base {};
struct A : public Base
{
void print()
{
cout << "A" << endl;
}
};
struct B : public A, public Base
{
void print()
{
A::print();
//error (ambiguous), no way to access to Base::print => warning
//Base::print();
}
};
struct C : public A, public Derived
{
void print()
{
A::print();
Derived::print(); // Not Ambiguous, it's the Base inherited by 'Derived' which is used.
// Still an error but you can access print indirectly through "Derived" => no warning needed
//Base::print();
}
};
int main()
{
B b;
b.print();
C c;
c.print();
return 0;
}