Regular cast vs. static_cast Vs.dymamic_cast in C + +

Source: Internet
Author: User

Describes the use of C + + type conversions.

1.static_cast

Static_cast is used with a forced implicit type conversion, that is, the inverse direction of an implicit type conversion. static_cast, limited conditions, no run-time inspection.

It must be used when you know the specific type of object, so the test is unnecessary, otherwise, unsafe.

Example

void func (void *data) {  //Conversion from myclass*-void* is implicit MyClass  *c = STATIC_CAST<MYCLASS*&G t; (data);  ...} int main () {  MyClass C;  Start_thread (&func, &c)  //func (&C) would be called      . Join ();


In this example, you already know that a MyClass object is being passed in, and there is no need for runtime validation.

What happens if a non-MyClass object is passed into the Func, see code below,

#include <iostream>using namespace Std;class a{public:a () {i=0;j=12;} int i;int J;}; void Cast_f (void *ptr) {A * p=static_cast<a *> (PTR);cout<<p->i<<endl;cout<<p->j< <endl;} int main () {int a=11;int * P=&A;CAST_F (P); cin.get (); return 0;}

Output 11 and an unknown. Instead of 0 and 12. This requires that we ensure that the object type is known to be passed in. Otherwise, it is not secure.

2.dynamic_cast

Dynamic_cast is used when you do not know what the object's dynamic type is, and you want to perform an operation function on the inheriting class on a class object that you have identified as inheriting, but there is only one pointer or reference to the base class object on your hand. If downcast and the type parameter is not polymorphic, dynamic_cast cannot be used and the compiler will give an error.

dynamic_cast returns a null pointer if the type of the object is not the base class of the type that will be converted to. That is, a non-derived class object is converted to a base class object, or a base class object is converted to a derived class object, returning a null pointer.

#include <iostream>using namespace Std;class a{public:virtual void F () {cout<< "A" <<endl;}}; Class B:public a{public:void dynamic_f () {cout<< "called Dynamic_f ()" <<ENDL;}; int main () {a A; B *b=dynamic_cast<b *> (&a);//when downcast, if a is not object of class B,return null Pointer.if (!b) {cout<< ;" Null "<<ENDL;} else{cout<< "NOT NULL" <<ENDL;} A *aa=0;//the base class pointer B bb;aa=&bb;//points to the derived class//aa how to call Dynamic_f ()? Aa.dynamic_f () Of course not, compilation does not pass B *ba=dynamic_cast<b*> (AA); Ba->dynamic_f (); Cin.get (); return 0;}



In this example, the base class object is converted to a derived class object and a null pointer is returned.

Up-cast (up-conversion) is an implicit conversion and is valid for static_cast,dynamic_cast,without any cast.

3.Regular Cast

Regular cast is also known as C-style type conversion. C-style cast, combined with const_cast,static_cast,reinterpret_cast, but it is unsafe because dynamic_cast is not supported.

The

is usually cast with C-style when the number is converted, and others in C + + style. In addition, C + + style is easier to read.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.