Question

So, I found this pretty nice tutorial about events in c++:

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. As far as I understand this is impossible due to the inaccessibility of the = operator of 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 ? Or didn't I understand what he means by "wrapper" in this case ?

Was it helpful?

Solution

The only mention of type_info on that page is

TypeInfo is a simple wrapper around type_info class that lets us store it as a key in std::map.

C++11 has std::type_index in <typeindex> which fulfills exactly this role. 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.

The specification for std::type_index notes that it may contain a pointer to std::type_info to simplify implementation. "Wrapper" does not imply inheritance or direct membership.

An class which wraps by reference is also known as a proxy.

OTHER TIPS

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.

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;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top