Topic: Well, Did my friend steal ?
Author: kavuri@lips.ecn.purdue.edu (Surya N Kavuri )
Date: 16 Dec 1994 00:05:09 GMT Raw View
const Car& MyCar = RedHondaCivic;
// hopefully not const for too long ;-)
Car& MyFriendsCar = Car(MyCar);
// My friend like my car and bought an exact copy of it.
Since copy constructor and conversion operator have the same
name, how do I know which one I am using ?
// Now how do I know whether he has a copy of my car or
// whether he stole it and changed the name plate ?
How about if Car was no more than "typedef int Car" ?
On a lighter note:
Q) What did C++ say to Lisp ?
Long time no C
Surya N. Kavuri
(FIAT LUX)
Author: Uwe Tantz <c4037>
Date: Fri, 16 Dec 1994 13:03:44 +0100 (MET) Raw View
On 16 Dec 1994, Surya N Kavuri wrote:
> const Car& MyCar = RedHondaCivic;
> // hopefully not const for too long ;-)
>
>
> Car& MyFriendsCar = Car(MyCar);
> // My friend like my car and bought an exact copy of it.
This is dangerous. "Car(MyCar)" creates a temporary object calling
the copy-constructor of "Car". You cannot expect the lifetime of this
object to be longer than the assignment instruction.
This means your Reference (MyFriendsCar) will point into desert.
On some compilers this will work anyway, because destruction time of
temporaries is not really specified in c++. Some compilers will destruct
this just at the end of the function.
I suppose to use the old-style casting mechanism:
Car& MyFriendsCar = (Car&) MyCar;
I heard that in ANSI C++ there will be a proper definition of the
different castings (redefinition cast and so on...)
>
> Since copy constructor and conversion operator have the same
> name, how do I know which one I am using ?
> // Now how do I know whether he has a copy of my car or
> // whether he stole it and changed the name plate ?
>
> How about if Car was no more than "typedef int Car" ?
>
> On a lighter note:
> Q) What did C++ say to Lisp ?
> Long time no C
>
> Surya N. Kavuri
> (FIAT LUX)
>
>
+---------------------+--------------------------------------------------+
| Uwe-Krischna Tantz | Tel/Fax: +49 941 / 56 31 40 |
| Hackengaesschen 4 | EMail: uwe.tantz@physik.uni-regensburg.de |
| 93047 Regensburg | |
+---------------------+--------------------------------------------------+
| "God is REAL, unless declared INTEGER!" |
+------------------------------------------------------------------------+