Last active
August 29, 2015 13:56
-
-
Save kkdai/8887258 to your computer and use it in GitHub Desktop.
Regular problem which comes from multiple inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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