Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!sun-barr!rutgers!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Why can't access control be used to disambiguate between base class members? Message-ID: <9726@alice.UUCP> Date: 2 Aug 89 15:54:09 GMT References: <2408@ginosko.samsung.com> <6590221@hplsla.HP.COM> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 34 In article <6590221@hplsla.HP.COM>, jima@hplsla.HP.COM (Jim Adcock) writes: > // The real solution is name conflict resolution, as shown below: > class A{ public: int a; }; > class B{ private:int a; }; > class C : public A, public B {public: int& A_a; C():A_a(A::a){}}; > void g(C* pc) > { > pc->A_a = 1; // OK, we now know it refers to class A, > // you can stop holding your breath. :-) > } The following variation works too, which is particularly useful because it allows C::a to be used. class A{ public: int a; }; class B{ private:int a; }; class C : public A, public B {public: int& a; C():a(A::a){}}; void g(C* pc) { pc->a = 1; // OK, we now know it refers to class A } The one disadvantage of this scheme is that it introduces an extra level of indirection: C::a (or C::A_a in the previous example) is actually stored in memory as a pointer and that pointer is dereferenced each time C::a is used. Of course, if enough people do this, compiler writers will start optimizing away the reference member in such cases and the overhead will vanish. -- --Andrew Koenig ark@europa.att.com