There are mainly four kinds of casting available in C++ to support different casting operations and also to overcome the different casting problems in C.
Static_cast--
-To convert non polymorphic types.
-This cast type uses information available at compile time to perform the required type conversion.
-Static_cast is not as safe as dynamic_cast, because it does not have the run time check, for example, for ambiguous pointer, static_cast may return successful but a dynamic_cast pointer will fail.
Const_cast--
To add or remove the const-ness or volatile-ness type.
Const-ness removal also can be achieved by using the mutable specifier.
Dynamic_cast--
To convert polymorphic types.
The dynamic_cast operator performs type conversions at run time.
Example:-
struct A {
virtual ~A() { };
};
struct B : A { };
int main() {
B bobj;
A* ap = &bobj;
void * vp = dynamic_cast(ap);
}
Reinterpret_cast--
For type conversion of unrelated types.
Need more attentions for applying this casting.
Static_cast--
-To convert non polymorphic types.
-This cast type uses information available at compile time to perform the required type conversion.
-Static_cast is not as safe as dynamic_cast, because it does not have the run time check, for example, for ambiguous pointer, static_cast may return successful but a dynamic_cast pointer will fail.
Const_cast--
To add or remove the const-ness or volatile-ness type.
Const-ness removal also can be achieved by using the mutable specifier.
Dynamic_cast--
To convert polymorphic types.
The dynamic_cast operator performs type conversions at run time.
struct A {
virtual ~A() { };
};
struct B : A { };
int main() {
B bobj;
A* ap = &bobj;
void * vp = dynamic_cast
}
Reinterpret_cast--
For type conversion of unrelated types.
No comments:
Post a Comment