|
From: Fabian on 17 Apr 2008 07:56 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
From: Igor Tandetnik on 17 Apr 2008 08:18 "Fabian" <Fabian(a)discussions.microsoft.com> wrote in message news:B76FAB6E-A6A2-4354-9780-4E229F5A8758(a)microsoft.com > 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: > > static inline void PrintError(const char* source, const int& > numMsgParams, ...); > > void ErrorHandler::PrintError(const char* source, const int& > numMsgParams, ...) > { > 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); > > Having to provide the number of arguments to the method call looks > pretty error-prone over maintenance time to me. Is there an > alternative? If you are willing to limit yourself to some maximum number of parameters, try something like this: // Add as many parameters as needed. void PrintError(const char* source, const char* param1 = 0, const char* param2 = 0) { const char* params[] = {param1, param2}; const int nParams = sizeof(params) / sizeof(params[0]); for (int i = 0; i < nParams; ++i) { if (params[i]) cout << params[i]; } } -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Ulrich Eckhardt on 17 Apr 2008 08:25 Fabian wrote: > 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: [..] > class ErrorHandler > { > public: > static inline void PrintError(const char* source, const int& numMsgParams, > ...); > private: > explicit ErrorHandler(){}; > }; A few things up front: 1. Passing 'int' by reference is overkill. The typical implementation of references is to pass a pointer, which might even be larger than just passing the integer itself and it incurs the indirection overhead. 2. Not every '}' is followed by a ';'! 3. ErrorHandler::PrintError() - I'd remove at least one instance of 'Error' here. 4. Classes that only have static functions are conveniently replaced with namespaces. Anyway, back to the issue you actually came for, instead of adding various overloads, use templates: // no arguments void print( char const* msg); // one argument template<typename A1> print( char const* msg, A1 const& a1); // two arguments template<typename A1, typename A2> print( char const* msg, A1 const& a1, A2 const& a2); This is still some writing, but it's not that much, and how many additional arguments do you need anyway? Take a look at Boost.Format, btw! Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932
From: Fabian on 17 Apr 2008 09:13 Hello Uli, > 4. Classes that only have static functions are conveniently replaced with > namespaces. This one was just the first step. I thought about writing different implementations using the factory method pattern later. I.e. different methods of error output (cerr, log file, whatever...). How big is the performance loss for the virtual method call in this case? regards, Fabian
From: Ulrich Eckhardt on 17 Apr 2008 11:18 Fabian wrote: > Hello Uli, > >> 4. Classes that only have static functions are conveniently replaced with >> namespaces. > > This one was just the first step. I thought about writing different > implementations using the factory method pattern later. I.e. different > methods of error output (cerr, log file, whatever...). How big is the > performance loss for the virtual method call in this case? The answer to pretty much every performance question is: you have to measure it. Anyway, estimations are possible... So, typically, calling a function means fetching the address of that function from somewhere and then jumping to it. With a virtual function, all that changes is that the place where you read that function's address from is relative to the object. In other words, it's like writing to an int on the stack compared to writing an int via a pointer to it. Further, since we are talking about logging, the function call actually involves IO, which is by leagues slower than a simple memory accesses like virtual function calls. Summary: you won't notice the overhead. BTW: a very convenient feature of C++ IOStreams is the overloaded rdbuf() function. Using that, you can easily redirect a stream to stdout, a file or any other stream: int main() { std::ofstream out("program.log"); std::cout.rdbuf(out.rdbuf()); : The output via std::cout is now redirected to a file. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932
|
Next
|
Last
Pages: 1 2 3 Prev: problem using delphi dll in vc++ Next: A Windows object named "Instance"... |