![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
navigating(resolution) inside double diamond
Hello NG,
I am curious why the resolution doesn't work in the following case. $ g++ --version g++ (GCC) 4.2.0 #include <iostream> #include <cstdlib> using namespace std; // building double diamond struct A { int x; }; struct L : A {}; struct R : A {}; struct C : L, R { int p; }; struct LL : C { int q; }; struct RR : C {}; struct D : LL, RR {}; void working() { D d, * pd = &d; // d has 4 x-int's C * pc[2]; // d consists of 2 C's A * pa[4]; // and 4 A's pc[0] = (LL *)pd; // LL branch to C pc[1] = (RR *)pd; // RR branch to C cout << pc[0] << endl << pc[1] << endl; pa[0] = (L *)pc[0]; // L branch to A pa[1] = (R *)pc[0]; // R branch to A pa[2] = (L *)pc[1]; // L branch to A pa[3] = (R *)pc[1]; // R branch to A cout << pa[0] << endl << pa[1] << endl; cout << pa[2] << endl << pa[3] << endl; // assign all integers for(int i=0; i<4; i++) pa[i]->x = i; } void notWorking() { D d; //d.D::LL:C::L::A::x = 0; // <= this line d.D::LL::q = 0; //d.D::LL::C::p = 0; // and this line } int main() { working(); notWorking(); return EXIT_SUCCESS; } g++ says: error: ‘C’ is an ambiguous base of ‘D’ of cause it has 2 C bases, but the "path" to the p member is no more ambiguous. Is there something in the standard about this situation? How do other compilers handle this code? Regards, Daniel -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: navigating(resolution) inside double diamond
Schüle Daniel ha scritto:
> > // building double diamond > struct A { int x; }; > struct L : A {}; > struct R : A {}; > struct C : L, R { int p; }; > struct LL : C { int q; }; > struct RR : C {}; > struct D : LL, RR {}; > > void notWorking() { > D d; > //d.D::LL:C::L::A::x = 0; // <= this line > d.D::LL::q = 0; > //d.D::LL::C::p = 0; // and this line > } > > g++ says: > error: ‘C’ is an ambiguous base of ‘D’ > of cause it has 2 C bases, but the "path" to the p member is > no more ambiguous. The "path" information is discarded. When you write D::LL::C::L::A the compiler replaces all that with A. So d.D::LL::C::L::A::x is the same as d.A::x and that is ambiguous. > Is there something in the standard about this situation? It's in 3.4.5 in the standard. > How do other compilers handle this code? They should behave the same way. Ganesh -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: navigating(resolution) inside double diamond
Schüle Daniel schrieb:
> Hello NG, > > > I am curious why the resolution doesn't work in the following case. > $ g++ --version > g++ (GCC) 4.2.0 > > > #include <iostream> > #include <cstdlib> > > using namespace std; > > // building double diamond > struct A { int x; }; > struct L : A {}; > struct R : A {}; > struct C : L, R { int p; }; > struct LL : C { int q; }; > struct RR : C {}; > struct D : LL, RR {}; Are you sure that this is your structure? There is *no* diamond here at all. Each D has two C's, and four A's. Do you probably mean "virtual base class"? So long, Thomas -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: navigating(resolution) inside double diamond
> The "path" information is discarded. When you write D::LL::C::L::A the > compiler replaces all that with A. So d.D::LL::C::L::A::x is the same as > d.A::x and that is ambiguous. thank you for explanation Is there rationale behind that, such as simplification of the compiler or language? >> Is there something in the standard about this situation? > > It's in 3.4.5 in the standard. > >> How do other compilers handle this code? > > They should behave the same way. Regards, Daniel -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: navigating(resolution) inside double diamond
> Are you sure that this is your structure? There is *no* diamond here at > all. Each D has two C's, and four A's. Do you probably mean "virtual > base class"? I am aware of the "virtual" inheritance, but it would reduce common data members into one. I was trying to build something complex to see how to reach all the contained members. Regards, Daniel -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|