By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,478 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,478 IT Pros & Developers. It's quick & easy.

variable number of function arguments

P: n/a
Hi,
How do i declare and use a function in c++ with variable number of
arguments...

Help
sandX

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"sandSpiderX" <m7*********@gmail.com> writes:
Hi,
How do i declare and use a function in c++ with variable number of
arguments...

One way is to read up about "Default Arguments in C++ Functions",
but that might not solve all your problems.
Jul 23 '05 #2

P: n/a


Tim Love wrote:
"sandSpiderX" <m7*********@gmail.com> writes:
Hi,
How do i declare and use a function in c++ with variable number of
arguments...

i dont know any special ways in C++, but varargs are one way to do (in
C) and i believe its portable too

Jul 23 '05 #3

P: n/a
do u mean this:

int main(int argc, _TCHAR* argv[])
{
.....

or do u want to call a function like this:

int bum(int x, char g, string pop)
{
....
bumb(0, "", "only here")
or woot?

Jul 23 '05 #4

P: n/a
Ian
sandSpiderX wrote:
Hi,
How do i declare and use a function in c++ with variable number of
arguments...

Don't, find a better way! This hangover from C looses all of the type
safety provided by C++

Ian
Jul 23 '05 #5

P: n/a
"sandSpiderX" <m7*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,
How do i declare and use a function in c++ with variable number of
arguments...

If the arguments have the same type, you could pass the arguments in
a vector or list or something of that sort. Otherwise, stick to Ian's
advice.
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #6

P: n/a
An another solution is a string. write all parameters to a string,
like "x=10,name=ashu,y=20.05" and parse this string inside the
function.

Jul 23 '05 #7

P: n/a
upashu2 wrote:
An another solution is a string. write all parameters to a string,
like "x=10,name=ashu,y=20.05" and parse this string inside the
function.


Surely a map of strings with your favourite key type would be more flexible, easier, faster, than that?

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.