如何知道什么类型的对象指向基类指针? [英] How to know what type of object is pointing a base class pointer?

查看:64
本文介绍了如何知道什么类型的对象指向基类指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Cell的基类(其他类具有指向此类的对象的指针),以及3个派生类CellA,CellB和CellC.当一个单元格失效时,指针指向单元格,在其他情况下该单元格仍处于活动状态(A,B或C).如何知道在此刻该指针指向什么??

I have a base class named Cell (other class has a pointer to an object of this class), and 3 derivated classes CellA, CellB and CellC. When a cell is dead, the pointer points to Cell, in other case is alive (A,B or C). How to know what is pointing that pointer in this moment to act??

推荐答案

如果您的类是多态的,则可以使用

In case your classes are polymorphic You can use dynamic_cast.
dynamic_cast allows you to safely downcast pointers.
In your case, You can use dynamic_cast to check if the Base class pointer is pointing to a derived instance, dynamic_cast can report you that information.

这篇关于如何知道什么类型的对象指向基类指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆