Topic: Pointer-to-member - ISO standard compliant?
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/09/30 Raw View
>>>>> In article <324d6c2b.36810762@library.airnews.net>,
>>>>> rmashlan@r2m.com (Robert Mashlan) writes:
> It's not safe. If you have a pointer to member initialized to point
> at a member in Derived that does not exist in Base, using that pointer
> to member with an object that is not of type Derived is asking for
> trouble.
I'm not sure about this point. The problem you describe is the
problem of casting from B::* to D::* (that is describe in the FAQ),
but I can't see how this problem would affect casting from D1 B2::* to
B1 D2::*, that is the cast the original posting inteded to be able to
do. Notice that he intended to cast a pointer to a member of Base2
that has type Derived1, to a pointer to a member of Derived2 (safe,
well-defined) that has type Base1 (undefined, but I think it would be
safe). I just can't think of a way to subvert the typing system by
using such a cast...
class B1 {};
class D1 : public B1 {};
class B2 { public: D1 x; };
class D2 : public B2 {};
void foo() {
D1 B2::* Dxp_in_b2 = &B2::x;
D1 D2::* Dxp_in_d2 = Dxp_in_b2; // safe, well-defined
B1 D2::* Bxp_in_d2 = Dxp_in_d2; // why isn't this allowed, or is it?
// I've just noticed gcc does not complain about this, is this a bug
// or I didn't try to find this in the standard hard enough?
}
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Brian Elgaard <bre@simcorp.dk>
Date: 1996/09/26 Raw View
In the process of compiling old C++ code I found that MS VC++ 4.2
refuses to compile code that can be compiled with several other Unix and
PC compilers (e.g. Watcom C++ 10.6).
The code makes nasty use of pointers-to-members and I am not sure if the
problem is due to a bug in the MS compiler or if it is due to
non-compliance of all the other compilers.
A simplified version of the code follows:
// A class hierarchy which represents values:
class MYDATABASE
{
public:
};
class MYDATA : public MYDATABASE
{
public:
};
// A class hierarchy with classes which include values:
class MYBASE
{
public:
};
class MYCLASS1 : public MYBASE
{
public:
MYDATA MyData1;
};
class MYCLASS2 : public MYBASE
{
public:
MYDATA MyData2;
};
// An array of pointers to members to values which are members of
// other classes:
typedef MYDATABASE MYBASE::* MYDATABASEMEMBER;
struct MYSTRUCT
{
const char *pMyText;
MYDATABASEMEMBER DataMember;
};
MYSTRUCT pStruct[]=
{
{"Text1", (MYDATABASEMEMBER)&MYCLASS1::MyData1},
{"Text2", (MYDATABASEMEMBER)&MYCLASS2::MyData2}
};
The error reported is
error C2642: cast to pointer to member must be from related pointer to member
Sincerely
Brian Elgaard, SimCorp A/S, Copenhagen, Denmark
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/09/27 Raw View
>>>>> In article <52dha8$k6a@news.uni-c.dk>, Brian Elgaard
>>>>> <bre@simcorp.dk> writes:
> class MYDATABASE {};
> class MYDATA : public MYDATABASE {};
> class MYBASE {};
> class MYCLASS1 : public MYBASE {
> public:
> MYDATA MyData1;
> };
> class MYCLASS2 : public MYBASE {
> public:
> MYDATA MyData2;
> };
> typedef MYDATABASE MYBASE::* MYDATABASEMEMBER;
> (MYDATABASEMEMBER)&MYCLASS1::MyData1
There's no well-defined conversion from pointer to member of class T
of type Derived to pointer to member of class T of type Base, even if
Derived is subclass of Base. Pointer to member conversions rules do
not apply to the dereferenced type, only to the container class.
Anyway, I can see no reason to disallow such a conversion. As far as
I can see, it would be safe if it were allowed --- but would be much
more complicated if virtual inheritance were involved. It seems to me
that it would be enough to adjust the offset of the pointer-to-member.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br
Universidade Estadual de Campinas, SP, Brasil
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: rmashlan@r2m.com (Robert Mashlan)
Date: 1996/09/29 Raw View
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>There's no well-defined conversion from pointer to member of class T
>of type Derived to pointer to member of class T of type Base, even if
>Derived is subclass of Base. Pointer to member conversions rules do
>not apply to the dereferenced type, only to the container class.
>
>Anyway, I can see no reason to disallow such a conversion. As far as
>I can see, it would be safe if it were allowed --- but would be much
>more complicated if virtual inheritance were involved. It seems to me
>that it would be enough to adjust the offset of the pointer-to-member.
It's not safe. If you have a pointer to member initialized to point
at a member in Derived that does not exist in Base, using that pointer
to member with an object that is not of type Derived is asking for
trouble.
rm
---
Robert Mashlan R2M Software rmashlan@r2m.com
Internet Resources for Windows Developers http://www.r2m.com/windev/
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]