I am trying to make a function that accepts a variable amount of character sequences without having to specify how many arguments are being passed when I call the function. Here is the basic idea:
void Alphabetize(char *text, ...)
{
va_list list;
va_start(list, text);
std::vector<std::string> chunks;
for (int n=0; n < list.size(); n++)
{
std::string chunk = va_arg(list, char*);
chunks.push_back(chunk);
}
va_end(list);
std::string result;
//alphabetize chunks and print each one out on its own line
return result;
}
int main()
{
std::cout << Alphabetize("Wood","Cow","Plastic","Random");
return 0;
}
Cow
Plastic
Random
Wood
There isn't actually a size() method with va_list, but I need something similar. Is it even possible to make a function like Alphabetize()? I don't want to have to specify the number of arguments in the call by saying
The only way is to do just that - somehow specify the size as one of the parameters. You can't ask "is this memory a parameter to my function or something else" without having other memory to answer and say yes or no. Sometimes this is done internally (virtual inheritance), and other times it needs to be done by the programmer, which is this case. Although, there may be some assembly for it - I don't know.