Topic: Pointer to member of a derived class
Author: provided!no.email@uunet.uu.net ("David White")
Date: Sun, 20 Apr 2003 06:19:45 +0000 (UTC) Raw View
""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message
news:jFeoa.527332$L1.152900@sccrnsc02...
> "David White" <no.email@provided.ucar.edu> wrote...
> > struct B {};
> > struct D : B {};
> >
> > struct X
> > {
> > D d;
> > };
> >
> > void f()
> > {
> > D X::*p = &X::d; // okay
> > B X::*q = &X::d; // error
> > }
> >
> > Why is the B X::* case not allowed?
>
>
> Because 'X' has no member in of type 'B'.
To explain what I thought was obvious:
X x;
B *p = &x.d; // okay
x doesn't have a member of type B either, yet this is allowed. Why the
difference?
David
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Sun, 20 Apr 2003 20:06:57 +0000 (UTC) Raw View
"David White" <provided!no.email@uunet.uu.net> wrote...
> ""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message
> news:jFeoa.527332$L1.152900@sccrnsc02...
> > "David White" <no.email@provided.ucar.edu> wrote...
> > > struct B {};
> > > struct D : B {};
> > >
> > > struct X
> > > {
> > > D d;
> > > };
> > >
> > > void f()
> > > {
> > > D X::*p = &X::d; // okay
> > > B X::*q = &X::d; // error
> > > }
> > >
> > > Why is the B X::* case not allowed?
> >
> >
> > Because 'X' has no member in of type 'B'.
>
> To explain what I thought was obvious:
>
> X x;
> B *p = &x.d; // okay
>
> x doesn't have a member of type B either, yet this is allowed. Why the
> difference?
But here you're not obtaining a pointer to member here. You're
getting a pointer to object. Those are different concepts.
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: provided!no.email@uunet.uu.net ("David White")
Date: Sun, 20 Apr 2003 21:40:13 +0000 (UTC) Raw View
""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message
news:oIBoa.276271$OV.327888@rwcrnsc54...
> "David White" <provided!no.email@uunet.uu.net> wrote...
> > ""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message
> > news:jFeoa.527332$L1.152900@sccrnsc02...
> > > "David White" <no.email@provided.ucar.edu> wrote...
> > > > struct B {};
> > > > struct D : B {};
> > > >
> > > > struct X
> > > > {
> > > > D d;
> > > > };
> > > >
> > > > void f()
> > > > {
> > > > D X::*p = &X::d; // okay
> > > > B X::*q = &X::d; // error
> > > > }
> > > >
> > > > Why is the B X::* case not allowed?
> > >
> > >
> > > Because 'X' has no member in of type 'B'.
> >
> > To explain what I thought was obvious:
> >
> > X x;
> > B *p = &x.d; // okay
> >
> > x doesn't have a member of type B either, yet this is allowed. Why the
> > difference?
>
> But here you're not obtaining a pointer to member here. You're
> getting a pointer to object. Those are different concepts.
Not completely different. You only use a pointer to member because later you
want to use it to access an object. I asked about this because someone in
comp.lang.c++ really had a mixture of members of different classes, all with
the same base class, and wanted a pointer-to-member to point to any one of
them.
It is fundamental to OO polymorphism in C++ that you can point a base-class
pointer to an object of a derived class. As long as the compiler knows the
offset of the 'B' part of the object within the 'D' member of 'X' for any
conceivable inheritance tree of classes 'B' and 'D', there's no reason why a
B X::* should not be able to "point" to X::d.
I suspect that the reason was either that it was thought too rare to bother
with, or that the designers didn't want to assume that it would work with
all object-layout models that implementers might want to use. I don't know
much about object-layout models, but it seems to me unlikely that you would
ever need to look into an actual object to know where any base-class sub-obj
ect of it is. So that seems to leave couldn't-be-bothered as the reason, and
I wondered if I'd missed something more obvious.
David
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Sun, 20 Apr 2003 22:34:36 +0000 (UTC) Raw View
In article <x3Eoa.2336$e8.125243@nasal.pacific.net.au>, David White
<provided!no.email@uunet.uu.net> writes
>I suspect that the reason was either that it was thought too rare to bother
>with, or that the designers didn't want to assume that it would work with
>all object-layout models that implementers might want to use. I don't know
>much about object-layout models, but it seems to me unlikely that you would
>ever need to look into an actual object to know where any base-class sub-obj
>ect of it is. So that seems to leave couldn't-be-bothered as the reason, and
>I wondered if I'd missed something more obvious.
Then you need to consider such things as virtual bases where layouts get
to be very fluid.
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: no.email@provided.ucar.edu ("David White")
Date: Sat, 19 Apr 2003 03:02:24 +0000 (UTC) Raw View
struct B {};
struct D : B {};
struct X
{
D d;
};
void f()
{
D X::*p = &X::d; // okay
B X::*q = &X::d; // error
}
Why is the B X::* case not allowed?
David
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Sat, 19 Apr 2003 20:50:26 +0000 (UTC) Raw View
"David White" <no.email@provided.ucar.edu> wrote...
> struct B {};
> struct D : B {};
>
> struct X
> {
> D d;
> };
>
> void f()
> {
> D X::*p = &X::d; // okay
> B X::*q = &X::d; // error
> }
>
> Why is the B X::* case not allowed?
Because 'X' has no member in of type 'B'.
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]