用C ++进行转换 [英] Casting in C++
本文介绍了用C ++进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨 dynamic_cast和typeid运算符强制转换之间有什么区别?
Hi What are the Differences between dynamic_cast and typeid operator cast?
推荐答案
好吧,typeid并没有强制转换任何内容.它只是让您找出对象的类型.
dynamic_cast有点聪明,它只能让您从派生类强制转换为其基类,或者从基类强制转换为其派生类(假设基类对象实际上是派生类的)
如果您实际上打算像这样进行投射:
MyClass a = (MyClass)SomeObject;
这样的转换几乎可以允许任何操作.
Google推荐给您的一些方便阅读的内容:
使用c ++的类型转换运算符 [演示动态类型转换 [
Well, typeid doesn''t really cast anything. It just lets you find out the type of an object.
dynamic_cast is a little bit clever, it will only let you cast from a derived class to its base class, or from a base class back to its derived class (assuming the base class object is actually of the derived class)
If you actually meant about casting like this:
MyClass a = (MyClass)SomeObject;
Then that kind of cast will pretty much allow anything.
Some handy things for you to read that google threw up:
Type casting operators in c++[^]
Demonstrating dynamic cast[^]
这篇关于用C ++进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文