|
-
March 21st, 2003, 09:18 AM
#1
forwarding variable number of arguments
I want to write a general method that will add entries to a log file.
My problem is that i don't know how to pass all the arguments from logEvent() method to CString::Format()
void CXServerLogFile::logEvent( UINT resID, char *first, ... )
{
CString strEvent;
strEvent.Format( resID, .... ); // what do i put here?
strEvent += "\n";
writeEvent( strEvent );
}
-
March 21st, 2003, 09:41 AM
#2
I made some debuging through MFC to see how CString::Format() works, and i found the answer to my problem. Here is the code:
void CXServerLogFile::logEvent( UINT resID, ... )
{
CString strEvent;
CString strFormat;
strFormat.LoadString( resID );
va_list argList;
va_start( argList, resID );
strEvent.FormatV( strFormat, argList );
va_end( argList );
strEvent += "\n";
writeEvent( strEvent );
}
-
March 21st, 2003, 11:36 AM
#3
Using va_args is very unsafe (you lose all your type-checking and parameter validation by the compiler). You would probably be better off looking for a solution that uses function overloading, rather than this extremely ugly method.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 21st, 2003, 01:42 PM
#4
If all of the arguments are of the same type, I've always felt that this article by the Saint of the New Form ( ) worked fairly well. Otherwise, for different types, one should look into a type safe hetrogeneous container like a vector of boost::any, as found at www.boost.org and just pass one argument of this form...
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|