Solved

variable number of arguments

Posted on 2002-05-02
12
427 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

820 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