In my program I often need to use parameters to create log messages and to create string commands. For example:
Code:
printf("Time of event %s:\nEvent: %s\n", event_time, description);
This is not real code, but illustrate what I mean. I had to create an exception class that allows me to create error messages with parameters in a straightforward manner, using va_args. This is the constructor:
Code:
Excecao::Excecao(const char* fmt, ...)
{
va_list listaParam;
va_start(listaParam, fmt);
setStr(fmt, listaParam);
va_end(listaParam);
}
void Excecao::setStr(const char* fmt, va_list args)
{
char buffer[1024];
vsnprintf(buffer, 1024, fmt, args);
_description = buffer;
}
The code is not perfect, I know. I do not know how to do it better. However, the point is: is this kind of construction possible in C++? I was changing char* to string, now I can't use sprintf to format my command in one line.
Thanks any suggestion or comment.