Solved

variable number of arguments

Posted on 2002-05-02
12
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 132
basic hardware to learn oop advanced design patterns 3 123
VS2015 Redefinition errors 4 104
Need some help with design 17 47
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

739 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