【发布时间】:2013-03-31 15:47:05
【问题描述】:
假设我们有一个类层次结构。在底部我们有 Base 和在顶部 Derived。 即使转换为基类指针,如何确定对象类。
Base* b = new Derived():
typeid(b).name(); // i want this to tell me that this is actually derived not base object
除了手动实现字符串字段或类似的虚拟获取功能之外,还有其他方法吗?
PS:我说的是独立于编译器的解决方案
【问题讨论】:
-
如答案中所述,这是可能的,但这几乎总是错误的做法。您想在这里实现什么目标?
-
没什么,只是为了争论:)
-
更多面向应用的版本:stackoverflow.com/questions/351845/… ||没有提及继承的更通用版本:stackoverflow.com/questions/11310898/…