zoukankan      html  css  js  c++  java
  • 【C/C++学习】之五、dynamic_cast

    dynamic_cast <new_type> (expression)

    可以安全的将类的指针或者引用沿着他们的继承层次转换!   但是指针必须有效,必须为0(可以对值为0的指针应用dynamic_cast,结果仍然是0)或者指向一个对象!  他只接受基于类对象的指针和引用的类型转换!
    在类层次上进行转换的时候 dynamic_cast于static_cast的效果一样!
    他返回一个新类型的值,或者会抛出一个异常!

    来看代码:
    #include<iostream>
    using namespace std;
    
    struct V {
        virtual void f() {};  // must be polymorphic to use runtime-checked dynamic_cast
    };
    struct A : virtual V {};
    struct B : virtual V {
      B(V* v, A* a) {
        // casts during construction
        dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B, results in B*
        dynamic_cast<B*>(a); // undefined behavior: a has type A*, A not a base of B
      }
    };
    struct D : A, B {
        D() : B((A*)this, this) { }
    };
     
    struct Base {
        virtual ~Base() {}
    };
     
    struct Derived: Base {
        virtual void name() {}
    };
     
    struct Some {
        virtual ~Some() {}
    };
    
    int main(void)
    {
    	D d;
    	A& a = d; //upcast  这里可以用dynamic_cast
    	D& new_d = dynamic_cast<D&>(a);//downcast
    	B& new_b = dynamic_cast<B&>(a);//sidecast
    
    	Base* b1 = new Base;
    	if(Derived* d = dynamic_cast<Derived*>(b1))
    	{
    		cout << "downcast from b1 to d succeful" << endl;
    		d->name(); 
    	}
    
    	Base* b2 = new Derived;
    	if(Derived* d = dynamic_cast<Derived*>(b2))
    	{
    		cout << "downcast from b2 to d successful" << endl;
    		d->name();
    	}
    
    	if(Some *d = dynamic_cast<Some*>(b1))
    	{
    		cout << "downcast from b1 to Some Successful" << endl;
    		//d->name();
    	}
    
    	delete b1;
    	delete b2;
    
    	return 0;
    }

    输出结果是:
    downcast from b2 to d successful
    	上面看到的是指针的转换,我们也可以用dynamic_cast将基类引用转换成派生类引用!
    		dynamic_cast<Type&>(val);
    	Type是派生类型,val是基类类型,  当val实际引用一个Type类型对象,或者val是一个Type派生类型的对象的时候,dynamic_cast操作才将操作数val转换为想要的Type&类型。
    还记得我们指针和引用的区别么,引用是不能为空的!
    总结:
    	1、从子类到基类指针的转换:static_cast和dynamic_cast都是正确地,所谓正确是指方法的调用和数据的访问输出是期望的结果,所谓成功是说转换没有编译错误或者运行异常;
    	2、从基类到子类:static_cast和dynamic_cast都是正确的,其中static_cast的结果是非空指针,dynamic_cast的结果是空指针;  这里,static_cast是错误的;
    	3、两个没有关系的类之间的转换:dynamic_cast返回空指针,而static_cast则是编译不通过;而reinterpret_cast编译通过,返回不是空,但是是错误的;
    1) If the type of expression is the exactly new_type or a less cv-qualified version of new_type, the result isexpression.
    2) If the value of expression is the null pointer value, the result is the null pointer value of type new_type
    3) If new_type is a pointer or reference to Base, and the type of expression is a pointer or reference to Derived, where Base is a unique, accessible base class of Derived, the result is a pointer or reference to the Base class subobject within the Derived object pointed or identified by expression. (note: implicit cast and static_cast can perform this conversion as well)
    4) If expression is a pointer or reference to a polymorphic type, and new_type is a pointer to void, the result is a pointer to the most derived object pointed or referenced by expression.
    5) If expression is a pointer or reference to a polymorphic type Base, and new_type is a pointer or reference to the type Derived a run-time check is performed:
    a) The most derived object pointed/identified by expression is examined. If, in that object, expressionpoints/referes to a public base of Derived, and if only one subobject of Derived type is derived from the subobject pointed/identified by expression, then the result of the cast points/refers to that Derivedsubobject. (this is known as the "downcast")
    b) Otherwise, if expression points/refers to a public base of the most derived object, and, simultanously, the most derived object has an unambiguous public base class of type Derived, the result of the cast points/refers to that Derived (this is known as the "sidecast")
    c) Otherwise, the runtime check fails. If the dynamic_cast is used on pointers, the null pointer value of typenew_type is returned. If it was used on references, the exception std::bad_cast is thrown.
    6) When dynamic_cast is used in a constructor or a destructor (directly or indirectly), and expression refers to the object that's currently under construction/destruction, the object is considered to be the most derived object. Ifnew_type is not a pointer or reference to the construction's/destructor's own class or one of its bases, the behavior is undefined.
    dynamic_cast却会进行判别,确定源指针所指的内容,是否真的合适被目标指针接受。如果是否定的,那么dynamic_cast则会返回null。
    
    
    
    
    
    2012/8/10
    jofranks 于南昌

  • 相关阅读:
    Programming Contest Ranking(题解)
    Alphabet Cookies
    hpu 1267 Cafeteria (01背包)
    Triangles 正多边形分割锐角三角形
    ACdream 1067:Triangles
    hdu 1253 胜利大逃亡 (代码详解)解题报告
    最短路
    POJ- 1511 Invitation Cards
    E
    HDU
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211418.html
Copyright © 2011-2022 走看看