va_start
initialises the va_list
to get arguments after the one specified, since in your example you are only sending one argument to the method, the va_list
is empty.
You also need to reinitialise the va_list
after calling vsnprintf
.
You need to rethink the signature of your method, could do a static function:
class String : public std::string {
public:
String(std::string str) : std::string(str) {}
String(char *str) : std::string(str) {}
static String Format(String format, ...) {
va_list argList;
va_start(argList, format);
// determine num of chars needed, don't store anything anywhere though!
size_t charsNeeded = vsnprintf(NULL, 0, format.c_str(), argList);
va_end(argList);
va_start(argList, format);
// print formatted string into buf
char buf[charsNeeded];
vsprintf(buf, format.c_str(), argList);
va_end(argList);
return String(buf);
}
};
EDIT:
If you are looking to format strings the best solutions is probably to use a std::stringstream
: http://www.cplusplus.com/reference/sstream/stringstream/stringstream/