Variable length argument list - How to understand we retrieved the last argument?
Posted
by
hkBattousai
on Stack Overflow
See other posts from Stack Overflow
or by hkBattousai
Published on 2011-01-09T21:41:17Z
Indexed on
2011/01/09
21:53 UTC
Read the original article
Hit count: 232
I have a Polynomial
class which holds coefficients of a given polynomial.
One of its overloaded constructors is supposed to receive these coefficients via variable argument list.
template <class T>
Polynomial<T>::Polynomial(T FirstCoefficient, ...)
{
va_list ArgumentPointer;
va_start(ArgumentPointer, FirstCoefficient);
T NextCoefficient = FirstCoefficient;
std::vector<T> Coefficients;
while (true)
{
Coefficients.push_back(NextCoefficient);
NextCoefficient = va_arg(ArgumentPointer, T);
if (/* did we retrieve all the arguments */) // How do I implement this?
{
break;
}
}
m_Coefficients = Coefficients;
}
I know that we usually pass an extra parameter which tells the recipient method the total number of parameters, or we pass a sentimental ending parameter. But in order to keep thing short and clean, I don't prefer passing an extra parameter.
Is there any way of doing this without modifying the method signature in the example?
© Stack Overflow or respective owner