【C/C++学习】之五、dynamic_cast

本文详细介绍了C++中dynamic_cast的操作原理与使用方法,包括如何安全地在继承层次间转换指针和引用,并展示了dynamic_cast与static_cast在不同场景下的表现差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 is expression.
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 type new_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. If new_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 于南昌

转载于:https://www.cnblogs.com/java20130723/archive/2012/08/10/3211418.html

C++中有四种强制类型转换,分别是reinterpret_cast、const_cast、static_castdynamic_cast。 reinterpret_cast用于进行底层的重新解释转换,可以将一个指针或引用转换为其他类型的指针或引用,甚至可以将指针转换为整数类型。但是使用reinterpret_cast需要非常小心,因为它是一种非常危险的操作,可能会导致未定义的行为。 const_cast用于移除const属性,可以将const修饰的变量或指针转换为非const类型。这可以用于去除const限定符,从而修改被const修饰的变量的值。 static_cast用于进行常见的类型转换,可以将一个表达式转换为另一种类型,例如基本数据类型之间的转换、父类指针或引用转换为子类指针或引用、以及void指针和任意类型的指针之间的转换。但是需要注意,在进行父类到子类的转换时,只有当父类指针或引用实际上指向了一个子类对象时,才能进行成功的转换。 dynamic_cast用于在继承关系中进行安全的向下转型(downcasting)。它可以将一个父类指针或引用转换为子类指针或引用,同时会进行类型检查,确保转换是安全的。如果转换失败,dynamic_cast会返回一个空指针或抛出一个std::bad_cast异常。 这四种强制类型转换在不同的场景下有不同的应用,可以根据具体的需求选择合适的转换方式。但是需要注意,在使用这些强制类型转换时,一定要谨慎和慎重,确保转换的安全性和正确性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++中的类型转换static_castdynamic_cast、const_cast和reinterpret_cast总结](https://download.youkuaiyun.com/download/weixin_38629976/12808232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [C++的四种类型转换reinterpret_cast/const_cast/static_cast /dynamic_cast](https://blog.youkuaiyun.com/salmonwilliam/article/details/113941785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C++四种cast转换(const_cast、static_castdynamic_cast、reinpreter_cast)类型转换运算符](https://blog.youkuaiyun.com/Dontla/article/details/130792118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值