Hi!
C++ Problem!
I want to check (in C++) if a vector which contains objects of abstract type A contains objects of type B, where B is a class derived from A.
Why?
The reason I need this is that I'm trying to implement a basic Entity-Component System in C++.
So every component type will be derived from a single abstract class which will let me store them all in the same vector.
But I do need some way of knowing what kind of components are attached to an entity. So I need a certain way of differentiating as well, and that's where I really need the help!
Here's an example piece of code to very simply illustrate what I'm trying to implement:
class Component
{}
class Rigidbody : public Component
{}
class Mesh : public Component
{}
....
class ContainerClass
{
public:
template<typename T>
bool contains(const T element) const
{
//Return whether or not elements contains an element of type T.
//which has to be a class derived from the Component class,
//but not of abstract type "Component".
//I also need a way of making sure T can only be of a type derived from Component.
}
inline void add(const Component &p_Component)
{
m_Components.push_back(p_Component);
}
private:
vector<Component> m_Components;
}
So in:
ContainerClass test;
B b;
test.add(b);
test.contains(B); Should be true.
test.contains(C); Should be false.
By the way, I know there are some similar questions to this one here in StackOverflow, but every single solution I've seen is programming-language specific and doesn't apply to C++.
At least as far as I know.
Thanks!