(ref.doc)bs 220793

Next maxtal 250793 Prev: ABMT 130793 Up: Usenet

From: [email protected] (Bjarne Stroustrup)
Newsgroups: comp.lang.c++
Subject: Re: casting to reference...
Date: 22 Jul 93 11:27:49 GMT
Organization: AT&T Bell Laboratories, Murray Hill NJ



[email protected] (Richard M. Hartman @ negligable) writes

 > What is the difference between
 > 
 > 	(SomeClass) x
 > 
 > and
 > 
 > 	(SomeClass &) x
 > 
 > ???

	(SomeClass) x

tries to make a new object of class SomeClass with x as its initializer.
It will succeed iff you can make a SomeClass from an object of x's type.
Saying

	(SomeClass) x

is equivalent to saying

	SomeClass(x)

On the other hand

	(SomeClass &) x

tries to make a reference of type SomeClass& to x. You could read it as

	``pretend x is a SomeClass''

It will succeed iff a &x can be cast to SomeClass*.

See ``The C++ Programming language (2nd edition)'' r.5.4.

 > My compiler seems to want one form in some places and
 > the other form in others.  (Both uses are intended to
 > "cast away constness" where "x" is of type SomeClass.)

Since (SomeClass)x makes a new object, only (SomeClass&)x
can be successfully used for that purpose.



automatically generated by info2www version 1.2.2.8