Witam, mam funkcję, która odczytuje argumenty podane w innej funkcji i następnie zapisuje je w buforze.
#include <string>
#include <stdarg.h>
#include <iostream>
void FormatString(char* buff, size_t buffSize, const char* format, va_list args)
{
int w = vsnprintf(buff, buffSize, format, args);
if (w == -1 || w >= (int)buffSize)
w = (int)buffSize - 1;
buff[w] = 0;
}
class Klasa
{
public:
char buffer[4096];
void Text(std::string text, ...)
{
va_list args;
va_start(args, text);
FormatString(buffer, 4096, text.c_str(), args);
va_end(args);
std::cout << buffer << std::endl;
}
};
int main(int argc, char* args[])
{
Klasa klasa;
klasa.Text("value = %0.3f", 5.453);
return 0;
}
Chciałbym żeby na ekranie został wypisany taki string: value = 5.453
, niestety jest wypisywany value = -nan
Nie chce robić tego templatem, bo musiałbym wszystkie metody w klasie robić w ten sposób, a nie chce żeby klasa składała się z template'ów.
Dlaczego tak się dzieje? W jaki sposób mogę osiągnąć pożądany efekt?