variable number of function arguments

S

sandSpiderX

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

Help
sandX
 
T

Tim Love

sandSpiderX said:
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.
 
G

gmramaswamy

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

Son of Sam

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?
 
I

Ian

sandSpiderX said:
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
 
J

Jakob Bieling

sandSpiderX said:
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.
 
U

upashu2

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.
 
B

Ben Pope

upashu2 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top