tags:

views:

251

answers:

4

I found in one article saying "static_cast is used for non-polymorphic type casting and dynamic_cast is used for polymorphic type casting". I understand that int and double are not polymorphic types.

However, I also found that static_cast can be used between base class and derived class. What does polymorphic type here mean? Some people says polymorphic type means the base class with virtual function. Is that right? Is this the only situation? What else? Can anybody could elaborate this for me more?

+11  A: 

static_cast can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived. This ensures that at least the classes are compatible if the proper object is converted, but no safety check is performed during runtime to check if the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, the overhead of the type-safety checks of dynamic_cast is avoided.

static_cast can also be used to perform any other non-pointer conversion that could also be performed implicitly, like for example standard conversion between fundamental types:

double d=3.14159265;
int i = static_cast<int>(d); 

dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.

Therefore, dynamic_cast is always successful when we cast a class to one of its base classes.

// dynamic_cast
#include <iostream>
#include <exception>
using namespace std;

class CBase { virtual void dummy() {} };
class CDerived: public CBase { int a; };

int main () {
 try {
    CBase * pba = new CDerived;
    CBase * pbb = new CBase;
    CDerived * pd;

    pd = dynamic_cast<CDerived*>(pba);
    if (pd==0) cout << "Null pointer on first type-cast" << endl;

    pd = dynamic_cast<CDerived*>(pbb);
    if (pd==0) cout << "Null pointer on second type-cast" << endl;

    } catch (exception& e) {cout << "Exception: " << e.what();}
    return 0;
 }

Compatibility note: dynamic_cast requires the Run-Time Type Information (RTTI) to keep track of dynamic types . Some compilers support this feature as an option which is disabled by default. This must be enabled for runtime type checking using dynamic_cast to work properly.

Virtual Functions are responsible for Run-time Polymorphism in C++. A class that has at least one virtual function has polymorphic type.

Read More....

Read this too. It has been clearly written thatA class that declares or inherits a virtual function is called a polymorphic class.

Prasoon Saurav
thanks so much for your answer. But I don't think you answered my question. My question is what's is polymorphic type. Not the difference between static_cast and dynamic_cast
skydoor
Class that has at least one virtual function. You have already answered it in your question.
Prasoon Saurav
If you have no other method to make virtual: make the dtor be virtual, which it should always be in this kind of example anyway.
Roger Pate
I did not copy it from your code. I copied it from the link that I have posted. I have already asked skydoor to read from the link that I have given for more details.
Prasoon Saurav
Ah, they're the ones to blame for using try-catch incorrectly here!
Roger Pate
+6  A: 
Roger Pate
so what's polymorphic type?
skydoor
Any type that can be used polymorphicly.
Roger Pate
+2  A: 

I think the full phrase is "polymorphic type casting". You are right that static_cast works on types that are not related by inheritance (double - int, etc) and the others answers point out how the casts work.

I don't think the statement implied the existence of a mythical polymorphic type though - just that static_cast also works on unrelated types. The statement was a little confusing though and it's good to clarify.

Daver
+1  A: 

Well, the answer is simple. A class having atleast one virtual function is called a polymorphic type. This can be only a destructor also.

So the following is a 'polymorphic type'.

struct Test {
  virtual ~Test();
};
Abhay
so the class without virtual function can always be casted using static_cast?
skydoor
@skydoor:No, read my post. static_cast can perform conversions between pointers to _related classes_, not only from the derived class to its base, but also from a base class to its derived.
Prasoon Saurav
Its the other way around, a `dynamic_cast` can be performed only for a class with atleast one virtual function!
Abhay