I want to store an array of Member Functions of different Classes. Just a repetition here:
Requirements:
- TypeOf Class
- Instance of Class containing the Function
- AddressOf the Member Function
- Member Function parameters
What I can store:
- Instance of Class containing the Function
- AddressOf the Member Function.
- Member Function parameters
Normally, you don't need to store the Class type whatsoever, since you can make an array pointer to the Class type. The reason I can't do that is because the Class types I take in are unknown and different. The Class is going to be used in many different projects where Class types are unknown.
I need to store different types of Classes into the Array/List, in which case I just stored the addresses of the classes into an array pointer.
My Problem: When I'm about to call the Member Function, I need to cast the Class address to the Class type, but I don't know what the type is to cast to.
Example Code (Not tested - written real quickly):
class A
{
public:
void test1(int i);
};
class B
{
public:
void test2(char c);
};
class Container
{
long* objects;
long* funcs;
int index;
public:
Container()
{
objects = new long[5];
funcs = new long[5];
index = 0;
}
template <class C, typename Types ...>
void Add(C *obj, void (C::*func)(Types ...))
{
objects[index++] = (long)obj;
funcs[index++] = (long)func;
}
typename <Types ...>
void Call(int inx, Types ... values)
{
void (*func)(Types ...) = (void (*)(Types ...))funcs[inx];
// This is where I've got trouble. I don't store the Class
// types, so I don't know what pointer Class type to cast
// the Class Instance address to.
(((*???)objects[inx])->*func)(values ...);
}
};
Thanks in advance. Ask ahead if there are any holes or any questions.
funcs[index++] = (long)func;is valid? If I'm not wrong you're casting a pointer-to-member function to along. That's illegal.