C++ question - functions taking dynamic number of arguments...

Superwormy

Golden Member
Feb 7, 2001
1,637
0
0
Some C++ functions I've noticed take a random length list of arguements, for instance the .NET Console::Write method, you can do:

Console::Write ("just one");
Console::Write ("one", "two", aString, "three");
Console::Write ("yet", "another", "example", "using", "bunch", "of", arguments");

etc. etc.

cout does the same, you can say:
cout << " string one " << "string two";

I want to write a function that can take any random number of arguments like this... how?

function myFunction ( [ what do I put for arguments, and how to I access them? ] )
{
// stuff goes here
}
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
What you need is an "ellipse" operator. I'm not too familiar with the syntax but Im sure you can find plenty of material on this.


And FYI, cout doesn't work that way. The reason it works is because << returns cout itself. So it's equivalent to doing something like this:


cout.write("one").write("two").write("three");

Where each write returns the cout stream itself.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Argo
What you need is an "ellipse" operator. I'm not too familiar with the syntax but Im sure you can find plenty of material on this.


And FYI, cout doesn't work that way. The reason it works is because << returns cout itself. So it's equivalent to doing something like this:


cout.write("one").write("two").write("three");

Where each write returns the cout stream itself.

Ah cool, good to know exactly how that works.

Also, in C++, you can have default arguments, which are somewhat similar to variable arguments, but generally have different uses:

int fooBar(int s, int d=5, char * f="hello") {
.....
}

I might be a tiny bit off, I'm not quite a C++ person. ;)
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
To use variable arguments in C/C++ you put an an ellipses (...) in the function prototype/header and then you use commands such as va_list, va_start and va_args to extract them.

It's too complicated to explain in a thread but if you look into any C/C++ guide for those keywords you'll find what you're looking for.

I've used variable arguments quite a few times and they're incredibly useful.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: BingBongWongFooey
Originally posted by: Argo
What you need is an "ellipse" operator. I'm not too familiar with the syntax but Im sure you can find plenty of material on this.


And FYI, cout doesn't work that way. The reason it works is because << returns cout itself. So it's equivalent to doing something like this:


cout.write("one").write("two").write("three");

Where each write returns the cout stream itself.

Ah cool, good to know exactly how that works.

Also, in C++, you can have default arguments, which are somewhat similar to variable arguments, but generally have different uses:

int fooBar(int s, int d=5, char * f="hello") {
.....
}

I might be a tiny bit off, I'm not quite a C++ person. ;)

You're close - you do use default arguments that way, however you do that in the function declaration not defintion. For example:

void myFunc(int a, int b = 10, int c = 11);

void myFunc(int a, int b, int c) {
// ....
}

Also, I'm not sure you can use default arguments with non primitive types, i.e. strings, arrays.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Argo

You're close - you do use default arguments that way, however you do that in the function declaration not defintion. For example:

void myFunc(int a, int b = 10, int c = 11);

void myFunc(int a, int b, int c) {
// ....
}
aah.

Also, I'm not sure you can use default arguments with non primitive types, i.e. strings, arrays.
For some reason, that occured to me as well, so I kept my example simple. ;)

edit: I don't even think char * foo = "hello" is correct, I think it'd have to be char foo[] = "hello", assuming you can even use arrays.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
I don't recommend that you use the ellipses to pass variable number of arguments. It's unsafe and doesn't really fit with the C++ philosophy.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: singh
I don't recommend that you use the ellipses to pass variable number of arguments. It's unsafe and doesn't really fit with the C++ philosophy.

What is unsafe about it?
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
Originally posted by: BingBongWongFooey
Originally posted by: singh
I don't recommend that you use the ellipses to pass variable number of arguments. It's unsafe and doesn't really fit with the C++ philosophy.

What is unsafe about it?

No type checking.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: BFG10K
To use variable arguments in C/C++ you put an an ellipses (...) in the function prototype/header and then you use commands such as va_list, va_start and va_args to extract them.

It's too complicated to explain in a thread but if you look into any C/C++ guide for those keywords you'll find what you're looking for.

I've used variable arguments quite a few times and they're incredibly useful.

exactly correct
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: PrincessGuard
Originally posted by: BingBongWongFooey
Originally posted by: singh
I don't recommend that you use the ellipses to pass variable number of arguments. It's unsafe and doesn't really fit with the C++ philosophy.

What is unsafe about it?

No type checking.

ROFL! a lot of C/C++ coders considers that an advantage, its not a type safe language to begin with, people pass around void * all the time to avoid type checking. it is the power and pitfall of C

 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: PrincessGuard
Originally posted by: BingBongWongFooey
Originally posted by: singh
I don't recommend that you use the ellipses to pass variable number of arguments. It's unsafe and doesn't really fit with the C++ philosophy.

What is unsafe about it?

No type checking.

ROFL! a lot of C/C++ coders considers that an advantage, its not a type safe language to begin with, people pass around void * all the time to avoid type checking. it is the power and pitfall of C

 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: Ameesh
Originally posted by: PrincessGuard
Originally posted by: BingBongWongFooey
Originally posted by: singh
I don't recommend that you use the ellipses to pass variable number of arguments. It's unsafe and doesn't really fit with the C++ philosophy.

What is unsafe about it?

No type checking.

ROFL! a lot of C/C++ coders considers that an advantage, its not a type safe language to begin with, people pass around void * all the time to avoid type checking. it is the power and pitfall of C


While some people do exactly what you say, it certainly doesn't make it right. The same guys also complain it's so hard to debug C++ programs!
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: singh
Originally posted by: Ameesh
Originally posted by: PrincessGuard
Originally posted by: BingBongWongFooey
Originally posted by: singh
I don't recommend that you use the ellipses to pass variable number of arguments. It's unsafe and doesn't really fit with the C++ philosophy.

What is unsafe about it?

No type checking.

ROFL! a lot of C/C++ coders considers that an advantage, its not a type safe language to begin with, people pass around void * all the time to avoid type checking. it is the power and pitfall of C


While some people do exactly what you say, it certainly doesn't make it right. The same guys also complain it's so hard to debug C++ programs!

it doesnt make it wrong either.

YOURE NOT THE BOSS OF ME!





lol