Thread: Where is va_args now?

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    Where is va_args now?

    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.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In C++-style, you'd probably use stringstreams for that sort of thing. Check out the FAQ.

    It is possible to use <cstdargs>, sprintf(), and any C function in C++, it's just not recommended.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I don't know cstdargs, I will take a look, thanks!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's C's <stdarg.h> in C++. And I meant <cstdarg>. You know it, you're using it.

    [edit] Check out http://faq.cprogramming.com/cgi-bin/...&id=1043284385 [/edit]
    Last edited by dwks; 07-02-2007 at 12:36 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If all the arguments are strings, you don't need sprintf or stringstreams. Just add the strings together with + or +=.

Popular pages Recent additions subscribe to a feed