My suggestion is to use abstract pure virtual class as Interfaces OR template policy. Your system can accept only components that inherit from a template specified interface OR that implements template policies. In this way your system will know that, no matter what, your component will implement the functions present in the interface. The system will be aware of the fact that your component has an Update function in this way.
you could do something like this:
template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };
it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do
TestSystem<> test;
then you can do
class Test
{
std::vector<Interface *> items;
public:
void AddComponent(T& param)
{
items.push_back(¶m);
};
void Update()
{
for (unsigned i = 0; i < items.size(); i++)
items[i]->Update();
}
};
test.AddComponent(objThatInheritsFromInterfaceTestComponent);OR
InterfaceTestComponent could, for example, have as contract an Update function.
template <class T>
class Test
{
std::vector<T *> items;
public:
void AddComponent(T& param)
{
items.push_back(¶m);
};
void Update()
{
for (unsigned i = 0; i < items.size(); i++)
items[i]->Update();
}
};
P.S.: you can also avoid to use the templates if you do not like it, you probably doThe second one does not need this levelthe use of abstraction
class TestSystem { public: void AddComponent(InterfaceTestComponent& param) {}; };
the interfaces, however the compiler will work as well :)not compile the code if T does not implement a public Update function.