I have something like:
class A
{
public:
A(B* b, int* i)
{
void* args[] = { (void*)&b, (void*)&i };
}
};
But I need it to be more generic, in the sense that I want to the constructor of A
to accept any number of variables, of any type.
How do I accomplish this with templates?
CodePudding user response:
You can write your constructor this way:
class A {
public:
template <class... Args>
A(Args*... pargs) {
void* args[] = { (void*)&pargs... };
}
};
Note that you are storing the address of the pointers, not the pointers themselves, in args
.
Also I wouldn't use that kind of things unless you have a very good reason to in actual code.
CodePudding user response:
First of all: Dont't do it! For me it looks like a design bug if you need to convert all and everything to void pointers. But ok, the code can be:
class A
{
public:
template < typename ... T>
A(T*... parms)
{
void* args[] = { (void*)&parms... };
}
};
But why we need pointer to pointer withoud having the types anymore... I have no idea!