读书人

求教 typeid .name解决思路

发布时间: 2013-09-05 16:02:07 作者: rapoo

求教 typeid .name

 const char* typeName = typeid().name();

得到的类型名称识别的字符串指针 typeName;
在程序运行起来后的任何时刻调用typeid().name()是否都是指向相同的地址? typeid
[解决办法]
typeid Operator
C++ Specific —>

typeid( type-id )

typeid( expression )

The typeid operator allows the type of an object to be determined at run-time.

The result of a typeid expression is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used. See type_info Class for more information.

END C++ Specific

type_info Class
C++ Specific —>

The type_info class describes type information generated within the program by the compiler. Objects of this class effectively store a pointer to a name for the type and an encoded value suitable for comparing two types for equality or collating order. The encoding rules and collating sequence for types are unspecified and may differ between programs.

class type_info {
public:
virtual ~type_info();
int operator==(const type_info& rhs) const;
int operator!=(const type_info& rhs) const;
int before(const type_info& rhs) const;
const char* name() const;
const char* raw_name() const;
private:
...
};
Note You must include the typeinfo.h header file in order to use the type_info class.

END C++ Specific



[解决办法]
如果内存没有擦除话,可能是一样!
[解决办法]
Notes
When applied to an expression of polymorphic type, evaluation of a typeid expression may involve runtime overhead (a virtual table lookup), otherwise typeid expression is resolved at compile time.
It is unspecified whether the destructor for the object referred to by typeid is executed at the end of the program.
[解决办法]
应该是一样的,因为返回引用,应该是返回全局变量的引用!!
外部或静态变量或表中数据的引用
对每种类型是唯一的,除非使用动态库!!!
当然==和这个无关,是定义了operator==的原故!!

读书人网 >C++

热点推荐