Topic: Implicit conversions for pointer-to-member
Author: gordan.palameta@canrem.com (Gordan Palameta)
Date: Mon, 11 Oct 93 13:29:00 -0400 Raw View
Let D be a derived class of B, and Foo is some other class.
Is this legal without an explict cast?
B Foo::* <-- D Foo::*
HP C++ 3.0 (based on cfront) seems to think so, IBM C++ and DEC C++
disagree.
I would think that this is really no different from the ordinary
B * <-- D *
and therefore should be allowed.
Most books only discuss the (quite different) case of:
Foo D::* <-- Foo B::*
Author: daniels@biles.com (Brad Daniels)
Date: Mon, 11 Oct 1993 22:01:56 GMT Raw View
In article <60.1231.4352.0N18601B@canrem.com>,
Gordan Palameta <gordan.palameta@canrem.com> wrote:
>Let D be a derived class of B, and Foo is some other class.
>
>
>Is this legal without an explict cast?
>
> B Foo::* <-- D Foo::*
>
>HP C++ 3.0 (based on cfront) seems to think so, IBM C++ and DEC C++
>disagree.
>
>I would think that this is really no different from the ordinary
>
> B * <-- D *
>
>and therefore should be allowed.
Language issues aside, the difference between the two cases is that
the conversion from D * to B * (or D to B - it's not clear which you're
talking about, but it doesn't matter anyway) may require additional code.
That code will not exist in a D::* member function, so when you invoke
the D::* function through a B::* member function, you will get an actual
D::* pointer interpreted as a B::*. This situation is usually benign in
a single inheritance situation, but you will almost definitely get in trouble
if D also inherits from something other than B.
- Brad
--
----------------------------------------------------------------------
+ Brad Daniels | "Let others praise ancient times; +
+ Biles and Associates | I am glad I was born in these." +
+ These are my views, not B&A's | - Ovid(43 B.C - 17 A.D) +
Author: gordan.palameta@canrem.com (Gordan Palameta)
Date: Fri, 15 Oct 93 01:56:00 -0400 Raw View
-> In article <60.1231.4352.0N18601B@canrem.com>
-> gordan.palameta@canrem.com (Gordan Palameta) writes:
-> >Let D be a derived class of B, and Foo is some other class.
-> >
-> >
-> >Is this legal without an explict cast?
-> >
-> > B Foo::* <-- D Foo::*
->
-> You are asking the wrong question. The correct question is:
-> "Is it legal WITH an explicit cast?"
-> (Its definitely illegal without an explicit cast)
->
->
-> >Most books only discuss the (quite different) case of:
-> >
-> > Foo D::* <-- Foo B::*
->
-> Because that is the safe conversion.
OK, I think this has been misinterpreted.
Note the two (entirely different) cases are:
B Foo::* <-- D Foo::* // Foo:: and Foo:: B* and D*
Foo D::* <-- Foo B::* // D:: and B:: Foo* and Foo*
Please note the difference. The second case is discussed in most
books, but it's the first case I'm interested in.
To recapitulate, HP C++ (based on cfront 3.0) allows the first case
without a cast, but DEC C++ and IBM C++ require it.
Since the first case involves pointers to members of _the same class_,
it should be no different from the garden-variety
B * <-- D *
No?
Author: jamshid@emx.cc.utexas.edu (Jamshid Afshar)
Date: 15 Oct 1993 12:28:00 -0500 Raw View
In article <60.1286.4352.0N186751@canrem.com>,
Gordan Palameta <gordan.palameta@canrem.com> wrote:
>OK, I think this has been misinterpreted. [...]
> B Foo::* <-- D Foo::* // Foo:: and Foo:: B* and D*
Ohhh. Yes, you have been misinterpreted. You're asking whether a
pointer to member (not member function) of class Foo can convert to
another pointer to member of the same class Foo.
class B {};
class D : public B {};
class Foo {
public:
B base;
D derived;
};
B Foo::* bmp; // pointer to a B member of Foo
bmp = &Foo::base; // okay
bmp = (B Foo::*) &Foo::derived; // okay
bmp = &Foo::derived; // ???
The conversion is definitely legal with a cast: ARM 5.4 "A pointer to
a member may be explicitly converted into a different pointer to
member type where the tw types are pointers to members of the same
class [...]". It doesn't seem to be legal without the cast. 4.8 says
"Note that a pointer to member is not a pointer to object or a pointer
to function and the rules for conversions of such pointers DO NOT
APPLY to pointers to member. In particular, a pointer to member
cannot be converted to a void*." [emphasis mine]
I'm not sure why the conversion is not allowed. It seems type-safe.
Anyone?
Jamshid Afshar
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Thu, 14 Oct 1993 16:17:40 GMT Raw View
In article <60.1231.4352.0N18601B@canrem.com> gordan.palameta@canrem.com (Gordan Palameta) writes:
>Let D be a derived class of B, and Foo is some other class.
>
>
>Is this legal without an explict cast?
>
> B Foo::* <-- D Foo::*
You are asking the wrong question. The correct question is:
"Is it legal WITH an explicit cast?"
(Its definitely illegal without an explicit cast)
>
>HP C++ 3.0 (based on cfront) seems to think so, IBM C++ and DEC C++
>disagree.
>
>I would think that this is really no different from the ordinary
>
> B * <-- D *
>
>and therefore should be allowed.
Its completely different. B* <-- D* is always safe,
similarly D::* <-- B::* is always safe. Notice the
order of D and B above is reversed!
>Most books only discuss the (quite different) case of:
>
> Foo D::* <-- Foo B::*
Because that is the safe conversion.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA