Solved

variable number of arguments

Posted on 2002-05-02
12
422 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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