Jump to content
  • Advertisement
Sign in to follow this  
ju

template and va_arg

This topic is 5154 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 = 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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!