#include <iostream>classFighter{};classNinja:publicFighter{};classZombie:publicFighter{};intmain(){ Ninja* ninja =new Ninja; Fighter* f1 = ninja;//implicit cast, upcast is fine Ninja* n = f1;//can't go back, errordelete ninja;return0;}
Ninja* n = f1 is an compile time error and can't go back why? Because f1 could have been a Zombie and not a Ninja. Look what happens here
So basically we must cast
(Ninja*)f2 is casting. But this is DANGEROUS!. If we try to access data members or functions that Ninja has and not Zombie it results in unexpected behaviour.
Dynamic Cast
source type is not polymorphic
Remember! Dynamic Casting only polymorphic types
Lets add a virtual function to Fighter
and now well get this
It know by looking at RTTI.
Static Cast
Let look at 3 examples
1st Example
or
to prevent this we can use static_cast<>() which gives you a compile time checking ability, C-Style cast doesn't.
2nd Example
The following should result in an error but doesn't
Now if we change this to a static_cast
3rd Example
The following should result in an error but doesn't
int main(){
Ninja* ninja = new Ninja;
Fighter* f1 = ninja;//implicit cast, upcast is fine
Fighter* f2 = new Zombie;
Ninja* n = f2; // NOT OK!!!!!
return 0;
}
invalid conversion from Fighter* to Ninja*
int main(){
Ninja* ninja = new Ninja;
Fighter* f1 = ninja;//implicit cast, upcast is fine
Fighter* f2 = new Zombie;
Ninja* n = (Ninja*)f2;
return 0;
}
#include <iostream>
class Fighter{};
class Ninja: public Fighter{};
class Zombie: public Fighter{};
int main(){
Ninja* ninja = new Ninja;
Fighter* f1 = ninja;//implicit cast, upcast is fine
Fighter* f2 = new Zombie;
Ninja* n = dynamic_cast<Ninja*>(f2);
return 0;
}
cannot dynamic_cast f2 (of type class Fighter*) to type class Ninja* (source type is not polymorphic)
class Fighter{
public:
virtual void getName(){}
};
#include <iostream>
using std::cout;
class Fighter{
public:
virtual void getName(){}
};
class Ninja: public Fighter{};
class Zombie: public Fighter{};
int main(){
Ninja* ninja = new Ninja;
Fighter* f1 = ninja;//implicit cast, upcast is fine
Fighter* f2 = new Zombie;
if(Ninja* n = dynamic_cast<Ninja*>(f1))
cout << "f1 is ninja\n";
else
cout << "f1 isn't ninja\n";
if(Ninja* n = dynamic_cast<Ninja*>(f2))
cout << "f2 is ninja\n";
else
cout << "f2 isn't ninja\n";
return 0;
}
f1 is ninja
f2 isn't ninja
int main(){
char c = 10; // 1 byte
int *p = (int*)&c; // 4 bytes
*p = 512; // run-time error: stack corruption
std::cout << *p;
return 0;
}
0
Segmentation fault
int main(){
char c = 10; // 1 byte
int *q = static_cast<int*>(&c); // compile-time error
return 0;
}
error: invalid static_cast from type char* to type int*
struct A {};
int main(){
A* a = new A;
int i = 10;
a = (A*) (&i); // NO ERROR! Undesired!
return 0;
}
struct A {};
int main(){
A* a = new A;
int i = 10;
a = static_cast<A*>(&i);
return 0;
}
invalid static_cast from type int* to type A*
struct A {};
struct B : A {};
struct C {};
int main(){
A* b = new B;
B* b2 = static_cast<B*>(b); // NO ERROR! SMART!
C* c = (C*)(b); // NO ERROR! undesired!
return 0;
}
struct A {};
struct B : A {};
struct C {};
int main(){
A* b = new B;
B* b2 = static_cast<B*>(b); // NO ERROR! SMART!
C* c = static_cast<C*>(b);
return 0;
}
invalid static_cast from type A* to type C*
#include <iostream>
using std::cout, std::endl;
struct Stru{
int x;
int y;
char c;
bool b;
};
int main(){
Stru s;
s.x = 10; s.y = 20; s.c='a'; s.b=true;
int* p = reinterpret_cast<int*>(&s);
cout << *p <<endl;
++p;
cout << *p <<endl;
++p;
char* c = reinterpret_cast<char*>(p);
cout << *c <<endl << std::boolalpha;
cout << *(reinterpret_cast<bool*>(++c)) << endl;
return 0;
}