Topic: Casting a const to non-const ?


Author: kavuri@lips.ecn.purdue.edu (Surya N Kavuri )
Date: 17 Dec 1994 15:46:30 GMT
Raw View
MSVisualC++ allowed me to do the following:

void CDerived::MyFunction(const CDerived& d)
{
  Cbase* base = (base *) d;
  ...
}

Is this legal with other compilers ?

Surya





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 18 Dec 1994 19:12:20 GMT
Raw View
kavuri@lips.ecn.purdue.edu (Surya N Kavuri ) writes:

>MSVisualC++ allowed me to do the following:

>void CDerived::MyFunction(const CDerived& d)
>{
>  Cbase* base = (base *) d;
>  ...
>}

>Is this legal with other compilers ?

No. You cannot cast a class object to a pointer (unless the CDerived
class contains a member "operator base*()").

You can do this:
    Cbase* base = (base *) &d; // must take the address
--
Steve Clamage, stephen.clamage@eng.sun.com