C++ 如何获取对象的类型
c++ how to obtain the type of the object?
我怎样才能获得以下typeid
:T& object
;其中T
是:template<class T>
。我正在使用 c++ 代码。我确实测试了:
const std::type_info& ObjT= typeid(object);
std::cout<<"******the objT is: "<<&ObjT<<std::endl;
但它崩溃了。为什么?
如果你想要一个人类可读的名称,请使用 name()
方法:
std::cout<<"******the objT is: "<<ObjT.name()<<std::endl;
你是否包括:
#include <typeinfo>
也许你应该发送
std::cout << typeid(object).name() << std::endl;
也许你正在做&&对象的事实存在问题。
看这里的例子
这是你在做的吗?
#include <typeinfo>
#include <iostream>
template <typename T>
struct S {
};
int main() {
S <int> a;
S <int> & object = a;
const std::type_info & ObjT = typeid( object );
std::cout << "******the objT is: "<< ObjT.name() << std::endl;
}
这适用于 GCC 4.5.1。
相关文章:
- 用C++中的CPerson(类)类型的对象初始化STL矢量
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- C++11 中不同类型的对象的 std::array 的替代方案
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- 数组中不同类型的对象并获取每个对象的类型
- E0322:不允许使用抽象类类型 " " 的对象
- 在 C++ 中按参数创建不同类型的对象?
- 可变参数宏:无法通过"..."传递非平凡可复制类型的对象
- 类型测试对象的动态数组的这两个声明之间的区别?
- "Materializing"已知类型的对象以进行C++类型推断
- C++:将未知类型的对象存储为成员变量的类
- T D[N] 总是声明数组类型的对象吗?
- 声明中类型和对象的相同标识符
- 如何将 cv::mat 对象从 python 模块传递到 c++ 函数并返回返回的 cv::mat 类型为对象?
- 基准、值、值类型、对象和对象类型(C++)
- 是否需要 nullptr_t 类型的对象?
- 将不同类型的对象存储在容器中
- 为什么内置类型的对象上的溢出会导致异常/未定义的行为?
- 错误:不允许使用抽象类类型的对象
- 将 const 类型引用对象注册为类成员对象C++