Skip to main content
added 12 characters in body
Source Link
Chris Dodd
  • 125.8k
  • 13
  • 144
  • 235

Assuming these classes are all dynamicpolymorphic/dynamic (which they need to be to use typeid like this), you can just use dynamic_cast instead:

template <typename T> T* GetComponent() {
    for (Component* c : _components) {
        if (T *tc = dynamic_cast<T *>(c)) return tc;
    }
    return nullptr;
}

Assuming these classes are all dynamic (which they need to be to use typeid like this), you can just use dynamic_cast instead:

template <typename T> T* GetComponent() {
    for (Component* c : _components) {
        if (T *tc = dynamic_cast<T *>(c)) return tc;
    }
    return nullptr;
}

Assuming these classes are all polymorphic/dynamic (which they need to be to use typeid like this), you can just use dynamic_cast instead:

template <typename T> T* GetComponent() {
    for (Component* c : _components) {
        if (T *tc = dynamic_cast<T *>(c)) return tc;
    }
    return nullptr;
}
Source Link
Chris Dodd
  • 125.8k
  • 13
  • 144
  • 235

Assuming these classes are all dynamic (which they need to be to use typeid like this), you can just use dynamic_cast instead:

template <typename T> T* GetComponent() {
    for (Component* c : _components) {
        if (T *tc = dynamic_cast<T *>(c)) return tc;
    }
    return nullptr;
}