0

I'm creating a GameObject class and i want to use the same way as unity to get the GameObject components: GetComponent<"component">()...

I tried:

    //Template function
        template<class TYPE>
        TYPE* GetComponent();

//Definition
        template<class TYPE>
        inline TYPE * GameObject::GetComponent()
        {
            for (list<Component*>::iterator it = componentsList.begin(); it != componentsList.end(); it++) {
                if ((TYPE*)*it == TYPE) {
                    return (TYPE*)*it;
                }
            }
            return nullptr;
        }

but it's not working. I know that problem is at "if ((TYPE*)*it == TYPE)" because it shows errors and i tried other ways but nothing... I'm not good at templates -.-".

Any idea to implement this?

Thanks.

  • "it shows errors" you need to tell us the exact errors it shows. – Scott Chamberlain Mar 31 '17 at 02:14
  • I answered similar question [here](http://stackoverflow.com/a/37580620/3785314). It shows how to implement `AddComponent` and `GetComponent` in C++. Also, make sure to post your error message in your questions. – Programmer Mar 31 '17 at 03:42
  • @Programmer thanks, it's a good way, My problem with this is that i need to have a list of components to draw them on the engine inspector (I'm creating an engine) and how i can do a list of a template class? :S – Tino Martin Mar 31 '17 at 12:28

0 Answers0