From 2070501112406069266 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,4e0e65235aa5eceb X-Google-Attributes: gidf78e5,public From: sbnaran@localhost.localdomain (Siemel Naran) Subject: Re: pointer to member derivation question Date: 1999/06/23 Message-ID: #1/1 X-Deja-AN: 492832345 X-NNTP-Posting-Host: arboria-80.slip.uiuc.edu Approved: stephen.clamage@sun.com (comp.std.c++) References: X-UID: 0000000001 X-Status: $$$T Organization: University of Illinois at Urbana-Champaign Reply-To: sbnaran@uiuc.edu Newsgroups: comp.std.c++ Originator: clamage@taumet On 23 Jun 99 01:25:27 GMT, Colin Rafferty wrote: > class Base { /* ... */ }; > > class Derived : public Base { /* ... */ }; > > class Holder > { > public: > Derived _derived; > }; > > void f() > { > Base Holder::* elt = &Holder::_derived; > } > >I don't have a copy of the standard, but the Dec 96 WP seems to say >nothing about it in either [dcl.mptr] or [conv.mem]. If I'm not mistaken, covariant types for pointer to members are not allowed. Of my two compilers, Comeau (an EDG compiler) does not allow it, whereas Egcs does. Allowing covariant types is no problem, but the implementor has to be slighly more careful. Consider struct A { int a; }; struct B : A { int b; }; struct C : B { int c; }; struct Holder { C c1,c2; }; void f2() { B Holder::*const elt=&Holder::c2; } The entity "&Holder::c2" means the address of the C part of the c2 of a Holder object. Assuming sizeof(int) is 4, then sizeof(A) is 4, sizeof(B) is 8, sizeof(C) is 12. Now suppose that the implementation arranges a C object so that the C-only part is first, the B-only part is next, the A-only part is next (most implementations put the A part first, then B, then C). Then "&Holder::c2" would be 12 -- meaning go 12 bytes from the start of the Holder object to get to the C part of c2. But the full expression "B Holder::*const elt=&Holder::c2" changes the meaning of the offset. Now we must get to the BA part of c2, which means that the offset is 16. -- ---------------------------------- Siemel B. Naran (sbnaran@uiuc.edu) ---------------------------------- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]