I understand that static_cast can convert between base and derived and between derived and base. dynamic_cast will check resulting object is a 'complete' object.
dynamic_cast uses RTTI feature. But how does static_cast work? What does 'compatible types' mean?
My question was really about what compatible types meant. But I am happy that have learnt something from posts. This sample demonstrates how the compiler interprets compatible types. Last few lines are most interesting. note interesting results.
#include <iostream>
#include <exception>
using namespace std;
class CBase { virtual void dummy() {} };
class CDerived: public CBase { public: CDerived() : a(20) {} int a; };
class CDerived2: public CBase { public: CDerived2() : b(7) {} int b; };
class CDerived3: public CBase { public: CDerived3() : c('A') {} char c; };
class CNotDerived { int doit() const { return 9; } };
int main () {
try {
CBase * pba = new CDerived;
CBase * pbb = new CBase;
CDerived * pd;
CNotDerived* pnot = new CNotDerived;
CDerived2* pd2 = 0;
CDerived2* pdx = new CDerived2;
CDerived3* pd3 = 0;
pd = dynamic_cast<CDerived*>(pba);
if (pd==0) cout << "Null pointer on first type-cast" << endl; //ok
pd = dynamic_cast<CDerived*>(pbb);
if (pd==0) cout << "Null pointer on second type-cast" << endl; //null ptr here
pd = static_cast<CDerived*>(pbb); //non-null pointer returned (not really what you want)
if (pd==0) cout << "Null pointer on third type-cast" << endl;
// pd = dynamic_cast(pnot); //error C2683: 'dynamic_cast' : 'CNotDerived' is not a polymorphic type // if (pnot==0) cout << "Null pointer on fourth type-cast" << endl;
// pd = static_cast(pnot); //error C2440: 'static_cast' : cannot convert from 'CNotDerived *' to 'CDerived *' // if (pnot==0) cout << "Null pointer on fourth type-cast" << endl;
//below lines compiled with ms vs2008 - I believe compiler SHOULD have flagged below as an error - but did not.
pd2 = static_cast<CDerived2*>(pba); //compiles ok but obviously incorrect
if (pd2==0) cout << "Null pointer on fourth type-cast" << endl;
cout << pd2->b << endl; //compiler had decided to give us CDerived->a value! Incorrect.
pd2 = static_cast<CDerived2*>(pdx); //compiles ok
if (pd2==0) cout << "Null pointer on fourth type-cast" << endl;
cout << pd2->b << endl; //gives correct value for b (7)
pd3 = static_cast<CDerived2*>(pdx); //error C2440: '=' : cannot convert from 'CDerived2 *' to 'CDerived3 *'
if (pd3==0) cout << "Null pointer on fourth type-cast" << endl;
cout << pd3->c << endl;
} catch (exception& e) {
cout << "Exception: " << e.what();
}
return 0;
}