CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2003
    Location
    Tg. Mures, Romania
    Posts
    11

    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 );
    }

  2. #2
    Join Date
    Mar 2003
    Location
    Tg. Mures, Romania
    Posts
    11
    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 );
    }

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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


  4. #4
    Join Date
    Sep 2002
    Posts
    1,747
    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
  •  





Click Here to Expand Forum to Full Width

Featured