Solved

variable number of arguments

Posted on 2002-05-02
12
410 Views
Last Modified: 2010-04-02
I want to pass variable number of arguments in C++ as  we do in C using va_list va_start etc...eg. printf ()

How to achieve this ?
0
Comment
Question by:vrelhan
12 Comments
 
LVL 2

Accepted Solution

by:
CSuvendra earned 50 total points
ID: 6987908
C++ is a super-set of C. So, you can always use va_list in functions in C++.

Maybe you already knew that. It would be better then if you give more details for your question.
0
 

Author Comment

by:vrelhan
ID: 6987920
do you mean to say that I can achieve this as I do it in C...I was just wondering if there is something different in C++ as regard to variable no of arguments.

0
 
LVL 4

Expert Comment

by:AmitAgarwal
ID: 6987935
no it is not. only thing is in C++ to pass a variable number of argument in a function the prototype should be
func(...) while in C only func() can do. in c++ it means it does not take any parameter.

amit
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6987976
Ellipsis is the name of that feature.

this is denoted by ... (3 dots )

You can find out many docuemnts related to this in MSDN

GOOD LUCK
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 6988023
If all of your arguments are the same type, you could do something like just having one argument, something like a std::vector of the values you wish to pass in
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 6988024
Technically, of course, passing a variable number of arguments to a C++ function is not something you should be doing--it completely destroys the type safety which is one of the central tenets of C++. If you know you're only ever going to have a limited number of different arguments (e.g. you might have functions which take a single int or two floats, but never anything else) then you'd probably be better of using overloaded functions.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:job_s
ID: 6988060
void WriteLog(char *pszLog,...)
{
     TCHAR chBuff[4096];
     va_list argList;
     va_start(argList,pszLog);
     vsprintf(chBuff,pszLog,argList);
     va_end(argList);

        //Formatted text will be on chBuff variable
        chBuff;

}


U can call this function like

WriteLog( "this is %s number %d", "simple test ", 1 );


regards
jobs
0
 

Expert Comment

by:Mithander
ID: 6989394
You could also use default values for your function
This is not really the same thing, but it may be what you
want.  Somthing like:

void Foo(int Bar = 0)
{ ... }

will allow you to call either
Foo()
or
Foo(x)

it type checks, but doesn't support things like printf's
variable args.

Mithander
0
 
LVL 1

Expert Comment

by:Yechezkel
ID: 6993629
If you want to pass or return a variable number of arguments in a convenient manner, you might want to check out the boost tuple library at http://www.boost.org/libs/tuple/doc/tuple_users_guide.html
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7011039
The general C++ model would be to pass a pointer to a Variant of type Array. But it depends exactly what you want to pass.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7178486
Dear vrelhan

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "CSuvendra"

comment(s) as an answer.

If you think your question was not answered at all, you can explain here why you want to do this and post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7199736
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now