I store a vector of base class pointers, I'm trying to dynamic cast it to a derived class pointer, there are several derived classes, none of the classes have any virtual methods.
The problem is that it dynamic casts based on a template argument, so regardless of what derived class it may have been when I added it to the vector of base class pointers, it can now be converted into any of the derived classes, losing all of its data which is obviously besides the point.
when adding a component it checks if the component has already been added, but there is no way to know because the base class pointer can be turned into any of its derived classes, so there will always only be 1 component, and even if I forcibly add components to the vector it will always return me the first component for the same reason.
does anyone have a solution for this? I'm trying to implement a component entity system.
//this is just a shell to allow me to store generic components in vectors
Component& operator=(const Component&);
const std::string name = "Component";
Well it looks like you are misusing dynamic_cast. If dynamic_cast succeeds then you know it is of the type T, so I don't really get why you are comparing the typeid after that. Not to mention it is a relatively costly operation you should only do it once and store the result instead of copy and pasting it everywhere. Also not sure about "hasComponent()" but it seems like it would do almost exactly the same thing as component() (iterate through the vector trying to dynamically cast to the type).
Can there only be one type of each specific component ?
you're right, I could just check if its nullptr or not.
right now I don't see a need to have more than one specific type of component, so yes.
I realize my implementation is pretty sucky right now, it's the result of some hacking and being revived from my recycling bin, but the main problem is how do I identify what the original derived class was when its stored in a vector of pointers to the base class.
after it is actually working I can then focus on optimizing it.