Hello,
for my error handling I have a class with static method overloads to print
error messages (I will probably make this a bit more general with some
pattern later). But for the start it is enough to print messages with
different numbers of arguments. I don't want to write endless overloads for
different numbers of arguments. So my first design looks like this:
// header file:
class ErrorHandler
{
public:
static inline void PrintError(const char* source, const int& numMsgParams,
...);
private:
explicit ErrorHandler(){};
};
// cpp file:
void ErrorHandler::PrintError(const char* source, const int& numMsgParams,
...)
{
cerr << source << ": ";
if (numMsgParams > 0)
{
va_list params;
va_start(params, numMsgParams);
char* addParam = NULL;
for (int i=0; i< numMsgParams; i++)
{
addParam = va_arg(params, char*);
cerr << addParam << ", ";
}
va_end(params);
}
cerr << endl;
}
Having to provide the number of arguments to the method call looks pretty
error-prone over maintenance time to me. Is there an alternative?
Thx for your suggestions,
Fabian