/* * TestEllipses.cpp */ #include #include using namespace std; int sum(int, ...); int main() { cout << sum(3, 1, 2, 3) << endl; // 6 cout << sum(5, 1, 2, 3, 4, 5) << endl; // 15 return 0; } int sum(int count, ...) { int sum = 0; // Ellipses are accessed thru a va_list va_list lst; // Declare a va_list // Use function va_start to initialize the va_list, // with the list name and the number of parameters. va_start(lst, count); for (int i = 0; i < count; ++i) { // Use function va_arg to read each parameter from va_list, // with the type. sum += va_arg(lst, int); } // Cleanup the va_list. va_end(lst); return sum; }