Sign in to follow this  

template and va_arg

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] = 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this