template and va_arg

Started by
4 comments, last by ju 19 years, 6 months ago
Hi everyone, I have this function in a template class:

CVector(X first, ...)
{
	array = new X[size];
	array[0] = first;	
	va_list args;
	va_start(args, first);
	for(int i(1); i < size; i++)
		array = va_arg(args, X);
	va_end(args);
}

And when I use it like this : CVector<double> V(1, 2, 3), it does not recognise that 1, 2, and 3 are doubles and it mess up the values... To make it work I have to do : CVector<double> V((double)1, (double)2, (double)3) I don't know if I'm clear enough... Do you have any ideas? Thanks in advance
Hey Ho Let's Go!
Advertisement
Quote:Original post by ju
Hi everyone,

I have this function in a template class:

*** Source Snippet Removed ***
And when I use it like this : CVector<double> V(1, 2, 3), it does not recognise that 1, 2, and 3 are doubles and it mess up the values... To make it work I have to do : CVector<double> V((double)1, (double)2, (double)3)

I don't know if I'm clear enough...
Do you have any ideas?

Thanks in advance


use 1. 2. and 3.

Also, just as a recommendation, do not use "..." as it will not work with non-pod types.
Really not a good idea. If you don't provide enough arguments, it won't work, but you won't know it isn't working.

size appears to be a constant. If it is, you should use a constructor with the maximum number of arguments, all with default values.
CoV
Thanks for your answers...
I think I'll change of methode...

for size : the class was in fact :
template <class X, int size> CVector
so size is not defined before the construction and then it was not possible to make a constructor with default values...


Polymorphic OOP : What are "non-pod" types?

Thanks in advance
Hey Ho Let's Go!
Plain Old Data

"POD (Plain Old Data) and Non–POD Objects"
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Plain Old Data

"POD (Plain Old Data) and Non–POD Objects"



Thank you very much Fruny!
Hey Ho Let's Go!

This topic is closed to new replies.

Advertisement