Link to home
Start Free TrialLog in
Avatar of GGRUNDY
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);
  }
Avatar of peterchen092700
peterchen092700

IIRC vararg-constructors are not allowed (but maybe I'm wrong, or your compiler is more forgiving)

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, ...)

??

ASKER CERTIFIED SOLUTION
Avatar of ramrajprabu
ramrajprabu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GGRUNDY

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
  }
Avatar of GGRUNDY

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. :-(
Avatar of Axter
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.