Original Post
hello, I am wondering whether this is a microsoft c++ compiler bug. I have written the following code: And the following main(): Now, if you look carefully, we have a GrandChild class which is inherited from both Child1 and Child2 classes. If you look at GrandChild class, basedata on its own (which is commented in this code) will not compile since it is ambigious (ie, compiler doesnt know which path to take for basedata). However, if I do Parent::basedata just like in the above code, it is still ambigious but the compiler does not complain!! When you run the code, you will notice Parent::basedata is taken from the Child1 path!! If you rearrange the inheritence order to Child2 then Child1, then Parent::basedata is taken from the Child2 path. Clearly, the compiler should output an error for the above code (specifically for the Parent::basedata variable in GrandChild class) instead of compiling successfully. Any thoughts?
#include <iostream>
using namespace std;
class Parent
{
protected:
int basedata;
public:
Parent()
{
basedata = 1;
cout << "Parent = " << this << endl;
}
void Show()
{
cout << basedata << endl;
}
};
class Child1 : public Parent
{
public:
Child1()
{
basedata = 2;
cout << "Child1 = " << this << endl;
}
void Show()
{
cout << basedata << endl;
}
};
class Child2 : public Parent
{
public:
Child2()
{
basedata = 3;
cout << "Child2 = " << this << endl;
}
void Show()
{
cout << basedata << endl;
}
//int basedata;
};
class GrandChild : public Child1, public Child2
{
public:
GrandChild()
{
Child1::basedata = 2;
Child2::basedata = 3;
Parent::basedata = 4;
//basedata = 4;
cout << "GrandChild = " << this << endl;
}
};
int main()
{
Child1 Ch1;
Child2 Ch2;
Ch1.Show();
Ch2.Show();
GrandChild G;
//G.ShowAdress();
G.Child1::Show();
G.Child2::Show();
G.Child1::Parent::Show();
G.Child2::Parent::Show();
return 0;
}