R
Rahul
Hi,
Everywhere I read that static_cast<> only work fine for the conversion
which are implicitly allowed by the compiler
hence the following does not work
int *i;
double *d;
d = i; // Compilation error ,
OK
i= static_cast<int *>(d); // Compilation error. OK
But surprisingly the following static_cast from Base * to Derived *
works (assume class Derived: public Base)
Base *bp=new Base();
Derived *dp=new Derived();
bp= dp; // Error, compiler does not allow this, This is OK
dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
compiler allow this (I tested in VC++ and gcc)
Everywhere I read that static_cast<> only work fine for the conversion
which are implicitly allowed by the compiler
hence the following does not work
int *i;
double *d;
d = i; // Compilation error ,
OK
i= static_cast<int *>(d); // Compilation error. OK
But surprisingly the following static_cast from Base * to Derived *
works (assume class Derived: public Base)
Base *bp=new Base();
Derived *dp=new Derived();
bp= dp; // Error, compiler does not allow this, This is OK
dp=static_cast<Derived*> (bp); // COMPILES FINE. But why does
compiler allow this (I tested in VC++ and gcc)