I have a situation where I need to create instances of a class with many object pointer parameters. But, I'm looking for a simpler way. In this code sample, I demonstrate very generically what I'm trying to do.
#include <vector>
#include <string>
class A
{
public:
A(std::string param);
};
class B
{
public:
B(std::vector<A*> params);
};
int main(int argc, char* argv[])
{
std::vector<A*> my_arguments;
my_arguments.push_back(new A("A1"));
my_arguments.push_back(new A("A2"));
my_arguments.push_back(new A("A3"));
my_arguments.push_back(new A("A4"));
B my_b = new B(my_arguments);
}
I don't like having to use an additional 5 lines to create the parameters for this call. Is there an alternate way to do this that requires less code. I tried using an array like so, but it doesn't seem to be working:
B my_b = new B([new A("A1"), new A("A2"), new A("A3"), new A("A4")]);
How could I achieve a similar result? I thought about using va_list
, but that would require another parameter marking the end, or a count of the number of variables. It might be what I end up doing, but, are there any other strategies out there?