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.