How to make a Function with Variable Argument List (type unknown)

K

Kapt. Boogschutter

I'm trying to create a function that has at least 1 Argument but can also
contain any number of Arguments (except 0 because my function would have no
meaning for 0 argument).

The arguments passed to the function are strings or must be (automaticly
converted to a string e.g. the number 10 should become the string "10".

My problem is that I can only find samples and description of printf() like
functions where the optional arguments and types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX
 
J

Jack Klein

I'm trying to create a function that has at least 1 Argument but can also
contain any number of Arguments (except 0 because my function would have no
meaning for 0 argument).

The arguments passed to the function are strings or must be (automaticly
converted to a string e.g. the number 10 should become the string "10".

My problem is that I can only find samples and description of printf() like
functions where the optional arguments and types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX

Let me get this straight. You are trying to write a function that can
accept one or more arguments. These arguments are all strings, and
the first string does not contain information about how many other
strings there might be. Is that correct?

In that case, if I call:

your_function("one");

your_function("one", "two");

your_function("one", "two", "three");

....how are you going to write your function so that when it sees "one"
it knows whether or not there is a "two" or a "three"?

The standard header <cstdarg> provides a mechanism for a function to
access a variable argument list, but there must be a mechanism for it
to determine what the arguments are (how many and what type). If you
don't want the information in the arguments before the variable ones
to tell you, you must use some kind of special end item.

If your function must be called like this:

func("one", (char *)0);

func("one", "two", (char *)0);

func("one", "two", "three", (char *)0);

....with the null pointer indicating the end of the list, you can still
use <cstdarg.h>.

For sure your function must have some way of knowing how many
parameters are passed, because if it tries to go past the number you
get undefined behavior, and probably a crashed program.
 
V

Victor Bazarov

Kapt. Boogschutter said:
I'm trying to create a function that has at least 1 Argument but can also
contain any number of Arguments (except 0 because my function would have no
meaning for 0 argument).

The arguments passed to the function are strings or must be (automaticly
converted to a string e.g. the number 10 should become the string "10".

My problem is that I can only find samples and description of printf() like
functions where the optional arguments and types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.

If specifying the number of arguments [implicitly] is not the case with
your function, how the hell will it know how many arguments were passed
to it?
Can anyone point me in the right direction?

Not until you give a more specific description of what you're trying to
accomplish.

V
 
J

John Harrison

Kapt. Boogschutter said:
I'm trying to create a function that has at least 1 Argument but can also
contain any number of Arguments (except 0 because my function would have no
meaning for 0 argument).

The arguments passed to the function are strings or must be (automaticly
converted to a string e.g. the number 10 should become the string "10".

That is impossible in C++.
My problem is that I can only find samples and description of printf() like
functions where the optional arguments and types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.

Well I have no idea how you intend to work out how many arguments there are,
it must be specified by the caller in some manner.
Can anyone point me in the right direction?

I think you are asking too much. What is this function going to do exactly?

john
 
J

JKop

Kapt. Boogschutter posted:
I'm trying to create a function that has at least 1 Argument but can
also contain any number of Arguments (except 0 because my function
would have no meaning for 0 argument).

The arguments passed to the function are strings or must be
(automaticly converted to a string e.g. the number 10 should become the
string "10".

My problem is that I can only find samples and description of printf()
like functions where the optional arguments and types are specified
within the first argument.

Since this is not the case with my function I have no idea how to make
this function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX



What a disgusting function.


I myself would go with something like the following:


int Pig(unsigned char amount_strings, char** pArrayOfPointersToStrings)
{
for(unsigned char i = 0; i < amount_strings; i += 1)
{
WantsString(pArrayOfPointersToStrings);
}
}


As for converting integers to strings, perhaps use the string class and have

string* pArrayOfStrings

Or use standard library functions.


-JKop
 
R

Rolf Magnus

Kapt. Boogschutter said:
I'm trying to create a function that has at least 1 Argument but can
also contain any number of Arguments (except 0 because my function
would have no meaning for 0 argument).

The arguments passed to the function are strings or must be
(automaticly converted to a string e.g. the number 10 should become
the string "10".

My problem is that I can only find samples and description of printf()
like functions where the optional arguments and types are specified
within the first argument.

That's for a reason. You can write functions with variable argument
lists, but a lot of the usual C++ functionality will be gone. C++ won't
give your function any hint about how many arguments were given or
which type they are. Your function will "just have to know", which
means you have to give that information explicitly to the function.
Further, C++ won't do any automatic conversion if you're trying to
interpret a parameter as another type than it acutally is, and you'll
receive no warning or error message. Usually, it will result in a crash
if you're lucky. And you can only pass POD types as variable arguments.
Basically, better avoid variable argument lists.
 
E

edA-qa mort-ora-y

Kapt. Boogschutter said:
Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.
Can anyone point me in the right direction?

You are probably looking for a more natural way to specify the list of
strings. In which case, there is probably and okay solution, which I've
seen work for templates before.

You can create a custom type, say StringBuffer, which overloads the ','
comma operator and has an implicit CTOR that takes (const char*). The
comma operator could then append a string to the StringBuffer. A
function something like this

StringBuffer& operator,( const char* str ) {
vector.push_back( str );
return *this;
}

Then your target function takes one parameter of type StringBuffer

void function( StringBuffer& buf ) ...

But you'll need to call your function like this:

function( ((StringBuffer)"one", "two", "three" ) );
function( ((StringBuffer)"one", "four" ) );
function( "one" ); //this case works from implicit conversion

Perhaps you can clean up the syntax, but they may make your program
less clear.

Attached is a working example.
 
K

Kapt. Boogschutter

Kapt. Boogschutter said:
I'm trying to create a function that has at least 1 Argument but can also
contain any number of Arguments (except 0 because my function would have no
meaning for 0 argument).

The arguments passed to the function are strings or must be (automaticly
converted to a string e.g. the number 10 should become the string "10".

My problem is that I can only find samples and description of printf() like
functions where the optional arguments and types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX
ThanX everyone for the insigth,

Your answers give me a little bit more to go on with.
I had hoped that I could get the length (number of arguments) of the list
with some kind of sizeof() function.
The Idea of using an array of JKop is interesting but since the size is
determined run-time I must use vectors instead.

ThanX everybody
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top