Skip to content

Instantly share code, notes, and snippets.

@kkdai
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkdai/8887258 to your computer and use it in GitHub Desktop.
Save kkdai/8887258 to your computer and use it in GitHub Desktop.
Regular problem which comes from multiple inheritance
#include <iostream>
#include <stdio.h>
class L
{
private:
int a1=1;
public:
L():a1(2), a2(a1) //Don't use like this, a2 result depends on compiler
{}
~L()
{ }
int a2=1;
};
class B1 : /*virtual*/ public L
{
private:
int b1=1;
public:
B1():b1(3), b2(b1) //Don't use like this, a2 result depends on compiler
{ }
~B1()
{ }
int b2=1;
int comm=0;
};
class B2 : /*virtual*/ public L
{
private:
int c1=1;
public:
B2():c1(4), c2(c1)
{
printf("C created\n");
};
~B2()
{
c1=0;
printf("C deleted\n");
}
int c2=1;
int comm=0;
};
class D : public B1, public B2
{
private:
int d=1;
public:
D()
{ }
~D()
{ }
};
int main(int argc, const char * argv[])
{
D *D1 = new D();
//D1->aClass::a2=2; //Ambiguous base class, it could using virtual in B1 and B2 when they inheritance from L
//D1->comm=1; //comm exist in multiple base case, ambiguous base case
D1->B1::comm =5; //It will work because it specific which variable using by :: (resolution operator)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment