- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的例子中,我不希望 stdout : Base Foo Bar
,但我得到 P4Base P4Base P4Base
:
#include <iostream>
#include <typeinfo>
#include <vector>
#include <memory>
class Base {};
class Foo : public Base {};
class Bar : public Base {};
using Collection = std::vector<std::unique_ptr<Base> >;
int main() {
Collection collection;
collection.push_back(std::make_unique<Base>());
collection.push_back(std::make_unique<Foo>());
collection.push_back(std::make_unique<Bar>());
for (auto &u:collection)
std::cout << typeid(u.get()).name() << std::endl;
}
有没有办法正确识别我的集合中有哪种实例?
struct Base {virtual ~Base() = default;};
struct Foo : public Base {};
struct Bar : public Base {};
using Collection = std::vector<std::unique_ptr<Base> >;
int main() {
Collection collection;
collection.push_back(std::make_unique<Base>());
collection.push_back(std::make_unique<Foo>());
collection.push_back(std::make_unique<Bar>());
for (auto &u:collection)
std::cout << typeid(*u).name() << std::endl;
}
最佳答案
Typeinfo how to get the name/id of the class in a polymorphic collection?
typeid
,类必须是多态的。提供动态类型。你的类不是多态的,所以你会得到静态类型。 typeid
to a pointer 为您提供指针类型的类型信息,而不是指向对象的类型。 typeid(*u).name()
最后,您对可读类名的期望是错误的。
std::type_info::name
不能保证给你你为类(class)写的名字。结果是实现定义的,在实践中,您通常会得到类型的重整名称。没有获得可读名称的标准方法,但是有实现定义的方法来对名称进行解构。
关于c++ - Typeinfo 如何在多态集合中获取类的名称/ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64884297/
以 TypeInfo 开头表示通用类型的对象,我想检索 TypeInfo s 代表泛型类型的每个类型参数。我想将其公开为具有此签名的扩展方法: public static TypeInfo[] Get
我在 https://issues.apache.org/jira/browse/KYLIN-2511 上发现了类似的错误 环境:hadoop-2.7.1hbase-1.3.2apache-hive-
我尝试将通用记录与 RTTI 结合使用,但遇到了类型信息问题。有谁知道为什么以下内容无法使用 Delphi 2010 进行编译? program GenericTypeInfo; {$APPTYPE
有没有办法在静态构造函数中获得等效的 GetType? 我想遍历静态构造函数中类型的可用属性,但 GetType 是一个实例方法?为什么是这样? typeinfo 应该存在于静态上下文中。有没有解决的
我有一个段错误。用gdb调试,栈中第一帧是 在 MyClass() 的类型信息中 有人知道类型信息吗? 最佳答案 有同样的问题;发布解决方案,因为它可能会帮助其他人: 彻底重建项目解决了这个问题——一
class A {} A a; type_info info = typeid (a); // error type_info is private 我想要一个列表 list存储类的类型。有解
我能做到: std::vector vec = { 1, 2, 3, 4, 5 }; 但是我不能这样做: std::vector ClassBlackList = { typeid(Class1),
对于需要泛型类型“系列”的一段代码,我尝试使用 TypeInfo 来检索所需的信息。 class function GetTypeKind:TTypeKind; 对于大多数类型我都能弄清楚。但匿名方法
在下面的例子中,我不希望 stdout : Base Foo Bar ,但我得到 P4Base P4Base P4Base : #include #include #include #inclu
我无法链接到动态库,该库定义了我需要使用的一组类。我的编译后的代码尝试链接到该类的typeinfo结构,但库未将其导出。但是,该类的虚拟表是。 使用nm,我找到了要解析的符号: U __ZTIN3ne
我正在尝试在 c++ 11 中使用 std::中的shared_ptr,因此我将其中显然使用了 typeinfo 的内容包括在内。问题是我收到以下错误。 In file included from /
是否有可能以某种方式获取 TypeInfo 对象中包含的类型的完全限定名称? 在调试器中,许多这些值很好地显示为 System.Int32,但是当它被打印出来时,没有一个包含这个完全限定的名称。我需要
在new Reflection API , TypeInfo.Declared* 属性是访问在类型上声明的成员(字段、属性、方法等)的正确方法。但是,这些属性不包括从基类继承的成员。 旧的 TypeI
我声明了以下枚举类型,我希望其中第一个成员的序数值为 1(一)而不是通常的 0(零): type TMyEnum = ( meFirstValue = 1,
我有以下类作为 JSON 对象的 java 表示: public class Vehicle { private VehicleType vehicleType; private Vehi
我试图通过在 C# 中使用 System.Reflection 将所有定义的类型检索到 .NET 程序集中。首先我加载程序集: var assembly = Assembly.LoadFrom("C:
当我使用 Metrics plugin 时,使用 JDK 1.7 运行 Eclipse Indigo ,我没有看到指标 View ,因此看不到任何指标。另外,当我尝试导出 -> 其他 -> 指标时,它
我遇到的问题与这里讨论的问题非常相似: g++ undefined reference to typeinfo 即便如此,我相信我也不会遇到同样的问题,而且该主题的答案并不能真正帮助我。我所拥有的是:
我正在阅读现代 C++ 设计。以下是摘自类型信息描述的文本片段。 The before member function introduces an ordering relationship for
在 C++ 中,我可以使用 typeid 运算符来检索任何多态类的名称: const char* name = typeid( CMyClass ).name(); 返回的 const char* 指
我是一名优秀的程序员,十分优秀!