First to say, I am making a game.
I need all objects to have intrusive lists with 1 global process, so that I call first object, and then each next performs it, until the process comes to the first object again. But the process is special for different objects of course.
I heard, template programming is cool, so I wanna use it, but I am not sure how. The question is about how to make this process global and special, benefitting from template programming efficience.
class Link {
private:
Link * prev;
Link * next;
public:
//...some possible stuff
virtualvoid globalProcess() = 0;
};
template <class T>
class Common : public Link {
public:
void globalProcess() {
static_cast<T*>(this)->specialProcess();
}
};
class Child : public Common<Child> {
void specialProcess() {
}
};
I heard, template programming is cool, so I wanna use it, but I am not sure how.
Template programming might be "cool", but that doesn't necessarily make it the right tool for this job.
It sounds like you just want a sort of 'master list' to house a bunch of objects? This sounds to me like you just want plain old polymorphism with your objects housed in a vector/list.
class Object
{
public:
static std::vector<std::unique_ptr<Object>> objectList;
virtual ~Object() {}
virtualvoid someProcess() = 0;
staticvoid callAllProcesses()
{
for(auto& i : objectList)
i->someProcess();
}
};
class ChildA : public Object
{
public:
virtualvoid someProcess() override
{
// do a thing specific to ChildA
}
};
class ChildB : public Object
{
public:
virtualvoid someProcess() override
{
// do a thing specific to ChildB
}
};
///////////////////////////////////////////
#include <vector>
#include <memory>
std::vector<std::unique_ptr<Object>> Object::objectList;
int main()
{
// create some objects, and add them to the main object list
Object::objectList.emplace_back( new ChildA() ); // <- add a ChildA object
Object::objectList.emplace_back( new ChildB() ); // <- add a ChildB object
Object::objectList.emplace_back( new ChildA() ); // <- add another ChildA object
// Then call each of those object's 'someProcess' function:
Object::callAllProcesses();
}