This question has probably been asked a million time, but here it comes
again. I want to learn the difference between the three type cast operators:
static_cast, reinterpret_cas t, dynamic_cast. A good way to do this is by
example. So I will give an example and please tell me what you think:
I have a base
class A
with a virtual destructor, and a
class B
that is it inherits publicly from A and defines som extra stuff.
If I have the following variables:
A* pA;
B* pB1 = new B();
B* pB2;
and the following assignment
pA = pB1;
which is most correct of the following
pB2 = static_cast< B* > (pA);
pB2 = reinterpret_cas t< B* > (pA);
pB2 = dynamic_cast< B* > (pA);
The last one works only if I have RTTI set.
Why should I use one of them isntead of the other? Please could you explain
this to me as if I were a 6 year old? I have an idea about it, but I am
finding it difficult to understand that idea, if you know what I mean.
And thank you very much
Jacob
again. I want to learn the difference between the three type cast operators:
static_cast, reinterpret_cas t, dynamic_cast. A good way to do this is by
example. So I will give an example and please tell me what you think:
I have a base
class A
with a virtual destructor, and a
class B
that is it inherits publicly from A and defines som extra stuff.
If I have the following variables:
A* pA;
B* pB1 = new B();
B* pB2;
and the following assignment
pA = pB1;
which is most correct of the following
pB2 = static_cast< B* > (pA);
pB2 = reinterpret_cas t< B* > (pA);
pB2 = dynamic_cast< B* > (pA);
The last one works only if I have RTTI set.
Why should I use one of them isntead of the other? Please could you explain
this to me as if I were a 6 year old? I have an idea about it, but I am
finding it difficult to understand that idea, if you know what I mean.
And thank you very much
Jacob
Comment