is there a way in modern C++ to "extend" a function? Maybe this pseudo code can explain what I want:
extendable void foo()
{
doSomething();
}
extend foo()
{
moo();
}
when compiled the function foo()
should do this:
foo()
{
doSomething();
moo();
}
I thought I could do this by storing every function that I want to have in foo()
in a std::vector of function pointers and foo()
will loop through the vector and run all functions.
However, I need to do this while compiling, thus I can't run vector.push_back()
.
I tried to do this with help of #define directives but I didn't find a solution.
Thanks in advance for any answer.
EDIT: My actual problem
I want to implement some kind of Entity-Component-System with a list for every component. Every entity has an ID that points to the list element in the component-list. Because I don't want to repeat the steps for creating a new type of component I wanted to make a #define-directive to make this automatically for every new component I'll create.
The main problem is to add to every component-vector one element if a new entity is created. This is the code I wanted to work:
#define addComponent(name) \
struct name; \
private: \
std::vector<name> name ## vector; \
public: \
extend addEntity() \
{ \
struct name a; \
name ## vector.push_back(a); \
} \
void set ## name(const int & ID, name newName) \
{ \
name ## vector[ID] = newName; \
} \
name get ## name(const int & ID) \
{ \
return name ## vector[ID]; \
}
class Component
{
public:
Component();
~Component();
extendable void addEntity(int * ID);
addComponent(position)
struct position
{
float x, y, z;
}
};