I got errors with GCC 4.4.6:
test.cpp: In function βvoid print(int, ...)β:
test.cpp:59: warning: cannot receive objects of non-POD type βstruct mtβ through β...β; call will abort at runtime
test.cpp: In function βint main()β:
test.cpp:83: warning: cannot pass objects of non-POD type βstruct mtβ through β...β; call will abort at runtime
test.cpp:83: warning: cannot pass objects of non-POD type βstruct mtβ through β...β; call will abort at runtime
test.cpp:83: warning: cannot pass objects of non-POD type βstruct mtβ through β...β; call will abort at runtime
test.cpp:83: warning: cannot pass objects of non-POD type βstruct mtβ through β...β; call will abort at runtime
$ g++
g++ (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3)
You cannot pass structparameters to the varargs function, skip pointers instead:
void print(int n, ...)
{
va_list ap;
va_start(ap, n);
for (int i = 0; i < n; i++)
{
mt *x(va_arg(ap, mt *));
switch (x->type)
{
case INT:
cout << x->val.i << endl;
break;
case DOUBLE:
cout << x->val.d << endl;
break;
case CHAR:
cout << x->val.c << endl;
break;
case STRING:
cout << x->val.s << endl;
break;
}
}
va_end(ap);
}
int main()
{
mt mt1(2);
mt mt2(4.2);
mt mt3('a');
mt mt4("Hello");
print(4, &mt1, &mt2, &mt3, &mt4);
}
What works great on my system:
$ ./a.out
2
4.2
a
Hello
source
share