GGRUNDY
asked on
va_list in a constructor
Hey guys(&gals) can someone please tell me which gotcha the following piece of code is contravening.
(Calls to the constructor go recursive at the return statement, indicating the stack is stuffed).
Cheers Greg
-------------------------- ---------- ---------- --------
class fmt:public string
{
public:
fmt(LPCSTR pzFmt , ...);
};
fmt::fmt(LPCSTR pzFmt , ...)
{
char z[1024];
va_list args;
va_start(args, pzFmt);
wvsprintf(z, pzFmt, args);
va_end(args);
operator=(z);
}
(Calls to the constructor go recursive at the return statement, indicating the stack is stuffed).
Cheers Greg
--------------------------
class fmt:public string
{
public:
fmt(LPCSTR pzFmt , ...);
};
fmt::fmt(LPCSTR pzFmt , ...)
{
char z[1024];
va_list args;
va_start(args, pzFmt);
wvsprintf(z, pzFmt, args);
va_end(args);
operator=(z);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks heaps, the following works just fine
fmt::fmt(LPCSTR pzFmt , ...)
{
char z[1024];
va_list args;
va_start(args, pzFmt);
wvsprintf(z, pzFmt, args);
va_end(args);
string::operator=(z); //call base assignement
}
fmt::fmt(LPCSTR pzFmt , ...)
{
char z[1024];
va_list args;
va_start(args, pzFmt);
wvsprintf(z, pzFmt, args);
va_end(args);
string::operator=(z); //call base assignement
}
ASKER
thanks peterchen. Your ideas put me on the right track. By the time I got back to the board to give you the points ramrajprabu had posted the exact answer. :-(
peterchen,
>>IIRC vararg-constructors are not allowed (but maybe I'm wrong, or your compiler is more forgiving)
What is "IIRC"?
And why do you think va_arg are not allow on constructors?
As far as I know, there is no constructor limitation for va_arg.
>>IIRC vararg-constructors are not allowed (but maybe I'm wrong, or your compiler is more forgiving)
What is "IIRC"?
And why do you think va_arg are not allow on constructors?
As far as I know, there is no constructor limitation for va_arg.
Anyway: Does fmt have it's own operator=(char const *) ? otherwise, the operator=(z) would construct a temporary fmt instance, using the LPCTSTR - constructor...
btw. why not using a global function:
string fmt(LPCTSTR, ...)
(or - if you need a specific class)
fmtstr fmt(LPCTSTR, ...)
??