Solved

variable number of arguments

Posted on 2002-05-02
12
416 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

22 Experts available now in Live!

Get 1:1 Help Now