简体   繁体   English

type_info的包装类

[英]Wrapper class for type_info

So, I found this pretty nice tutorial about events in c++: 所以,我发现这个关于c ++事件的非常好的教程:

http://www.gamedev.net/page/resources/_/technical/game-programming/effective-event-handling-in-c-r2459 http://www.gamedev.net/page/resources/_/technical/game-programming/effective-event-handling-in-c-r2459

BUT: the creator uses a wrapper for type_info returned by typeid. 但是:创建者使用typeid返回的type_info的包装器。 As far as I understand this is impossible due to the inaccessibility of the = operator of type_info. 据我所知,由于type_info的=运算符不可访问,这是不可能的。 Thus, I can't compile the code he provides. 因此,我无法编译他提供的代码。 Unfortunately, this is also the core part of the way his tutorial works. 不幸的是,这也是他的教程工作方式的核心部分。 Now, how would I get around this problem, should I simply use the type_info.hash_code - does this work to individually identify a class ? 现在,如果我只使用type_info.hash_code,我将如何解决这个问题 - 这是否可以单独识别一个类? Or didn't I understand what he means by "wrapper" in this case ? 或者我不明白在这种情况下“包装”意味着什么?

The only mention of type_info on that page is 该页面上唯一提到的type_info

TypeInfo is a simple wrapper around type_info class that lets us store it as a key in std::map. TypeInfo是一个围绕type_info类的简单包装器,它允许我们将它作为键存储在std :: map中。

C++11 has std::type_index in <typeindex> which fulfills exactly this role. C ++ 11在<typeindex>std::type_index ,它完全符合这个角色。 In general elements of a map do not need to be assignable, nor as of C++11 copyable, but type_info still cannot be used directly because you simply cannot construct one except by typeid expression. 通常, map元素不需要是可赋值的,也不需要C ++ 11可复制,但是type_info仍然不能直接使用,因为除了typeid表达式之外你根本无法构造一个。

The specification for std::type_index notes that it may contain a pointer to std::type_info to simplify implementation. std::type_index的规范指出它可能包含一个指向std::type_info的指针以简化实现。 "Wrapper" does not imply inheritance or direct membership. “包装”并不意味着继承或直接成员资格。

An class which wraps by reference is also known as a proxy. 通过引用包装的类也称为代理。

I asked myself the same question about this exact article, but then I realized that the code listing should have been provided, and it was. 我问自己同一个关于这篇文章的问题,但后来我才意识到应该提供代码清单,而且确实如此。 So TypeInfo is a straightforward wrapper around type_info . 所以TypeInfotype_info的直接包装器。

class TypeInfo{
public:
    explicit TypeInfo(const type_info& info) : _typeInfo(info) {};
    bool operator < (const TypeInfo& rhs) const{
        return _typeInfo.before(rhs._typeInfo) != 0;
    }
private:
    const type_info& _typeInfo;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM