Assume I have a class foo
with member function bar
:
// foo.hpp
class foo
{
...
virtual whatever_type bar() {...}
...
};
And class foo1
foo2
foo3
which is inherited from foo
:
//foo1.hpp
class foo1 : public foo
{
...
whatever_type bar() {...}
...
};
//foo2.hpp
class foo2 : public foo
{
...
whatever_type bar() {...}
...
};
//foo3.hpp
class foo3 : public foo
{
...
whatever_type bar() {...}
...
};
In the main function, if I want to create an object for each class foox
, I have to write it manually.
// main.cpp
#include ...
int main()
{
std::vector<foo> all_foo = {foo1(), foo2(), foo3()};
for(auto foo_object : all_foo)
foo_object.bar();
...
}
Is there any way to "register" a class when it is defined? So other functions will be able to get a list of all the registered classes.