What's the difference between typeid(*this).name() and typeid(this).name() in base class

622 Views Asked by At
class Event
{
public:
    virtual std::string getEventType()
    {
        return typeid(*this).name();
    }
}

class NotWorkEvent
{
public:
    virtual std::string getEventType()
    {
        return typeid(this).name();
    }
};
struct EventCallBak
{
    long funcId_;
    EventConsumeFunc func_;
};

class EventBus
{
public:
    EventBus()
        : callbackId_(0)
    {}

    void publish(std::shared_ptr<Event> event)
    {
        std::lock_guard<std::recursive_mutex> lock(mutex_);
        for (auto msgCb : callbackMap_)
        {
            if (event->getEventType() == msgCb.first)
            {
                for (EventCallBak cb : msgCb.second)
                {
                    cb.func_(event);
                }
            }
        }
    }
    template<typename T>
    long registerHandler(EventConsumeFunc func)
    {
        std::string type = typeid(T).name();
        std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
        EventCallBak cb;
        cb.funcId_ = callbackId_++;
        cb.func_ = func;
        auto it = callbackMap_.find(type);
        if (it == callbackMap_.end())
        {
            std::vector<EventCallBak> cbs;
            cbs.push_back(cb);
            callbackMap_[type] = cbs;
        }
        else
        {
            it->second.push_back(cb);
        }
        return cb.funcId_;
    }
    
    std::map<std::string, std::vector<EventCallBak>> callbackMap_;
    mutable std::recursive_mutex mutex_;
    long callbackId_;
    };

And here are the test code:

    class AnEvent : public Event
    {
    };

std::shared_ptr<EventBus> eventBus = std::make_shared<EventBus>();
int eventCaller = eventBus_->registerHandler<AnEvent>(
        [&](std::shared_ptr<Event> event)
        {
            std::shared_ptr<AnEvent> typedEvent = std::dynamic_pointer_cast<AnEvent>(event);
            EXPECT_TRUE(typedEvent != nullptr);
        });

Test result:

  1. if I define the EVENT as the "EVENT" class, then the AnEvent will be handled correctly.
  2. if I define the EVENT as the "NotWorkEvent" class, then the AnEvent will not be triggered.

My question:

1.What's the difference between typeid(*this), typeid(this) and also typeid(varname)?

2.Is that the same value (save to use with any compiler and C++ version) returned by typeid(T).name() in the template and typeid(*this) ?

1

There are 1 best solutions below

1
On

1.What's the difference between typeid(*this), typeid(this) and also typeid(varname)?

*this is an lvalue reference to the object and this is a pointer to the object. When you apply typeid on a reference, you get type info of the dynamic type of the referred object (if the type is dynamic). When you apply typeid on a pointer, you get type info of the pointer type.

2.Is that the same value (save to use with any compiler and C++ version) returned by typeid(T).name()

No. The name is implementation defined and is different across language implementations.