Topic: HELP: Pointer conversion
Author: Thomas.Gilbert@cern.ch (Thomas GILBERT)
Date: 1996/08/23 Raw View
Hey,
(I think I'm still a beginner)
I can't define a class member operator which allows pointer conversion between
a BASE class and one of its derived class... Here is what I've tried
(simplified) :
class Mother
{
...
operator Child*();
...
}
Mother::Child*()
{
return (Child*) this;
}
And when I use an instruction such :
pM* Mother;
pC* Child;
pC = pMother;
the compiler ignores the operator I defined and still says that there's any
conversion possible between "class Mother *" and "class Child *" ...
Can you help me?
Thanks a lot in advance!
[ 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: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/08/23 Raw View
In article <DwL25G.Jwt@news.cern.ch> Thomas.Gilbert@cern.ch (Thomas
GILBERT) writes:
|> I can't define a class member operator which allows pointer conversion between
|> a BASE class and one of its derived class...
You don't need an operator for this; it can be done by a simple cast.
|> Here is what I've tried
|> (simplified) :
|> class Mother
|> {
|> ...
|> operator Child*();
|> ...
|> }
|> Mother::Child*()
|> {
|> return (Child*) this;
|> }
First, the operator you have defined does not convert a pointer to
Mother (Mother*) to a pointer to Child, but rather converts a Mother
object (what is pointed to) to a Child*. This is *NOT* the same as
converting between two pointers.
Second, I don't see any inheritance. So it is a question of converting
between two unrelated classes. Unless Mother really does derive from
child, your operator is a sure recipe for core dumps, and other
disagreeable behavior. Within Mother, this points to an object of type
Mother. Saying it is a Child will not make it one.
If Mother actually does derive from Child, then you don't need a
conversion operator to convert the pointers; the compiler will do so
automatically. If Mother is a base class of child, you must explicitly
convert; in this case, I would recommend using dynamic_cast, if you
compiler supports it. In general, I would suggest using the new cast
syntax whenever it is available. If Mother is in fact unrelated to
Child (as a class), then the only cast that the compiler will allow is
reinterpret_cast; reinterpret_cast is only for very special systems
level programming, and the fact that no other cast works is a sign that
you are doing something wrong.
|> And when I use an instruction such :
|> pM* Mother;
|> pC* Child;
|> pC = pMother;
I presume you mean "pChild = pMother" here, where "pChild" and "pMother"
are defined as pointers to "Child" and to "Mother", respectively.
|> the compiler ignores the operator I defined and still says that there's any
|> conversion possible between "class Mother *" and "class Child *" ...
This is because your cast operator converts a "Mother", and not a
"Mother*", and in the expression in question, you are only using the
pointers. If Mother derives from Child, the assignment above is legal
as it stands; no further operators or whatever are necessary. If Child
derives from Mother, then you need to write:
pChild = dynamic_cast< Mother* >( pMother ) ;
You cannot define a new conversion operator between two pointers. You
can define new conversions (to or from class types), but you cannot
redefine existing conversions.
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, tudes et r alisations en logiciel orient objet --
-- A la recherche d'une activit dans une region francophone
[ 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 ]