|
|||||
|
|
#1 |
|
|
contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments p***ed 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 |
|
|
#2 |
|
|
<someone@nobody.com> wrote in comp.lang.c++: > 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 p***ed 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 p***ed, because if it tries to go past the number you get undefined behavior, and probably a crashed program. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html |
|
|
#3 |
|
|
> 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 p***ed 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 p***ed 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 |
|
|
#4 |
|
|
"Kapt. Boogschutter" <someone@nobody.com> wrote in message news:cb25ec$1cs$1@news4.tilbu1.nb.home.nl... > 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 p***ed 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 |
|
|
#5 |
|
|
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 p***ed 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[i]); } } As for converting integers to strings, perhaps use the string cl*** and have string* pArrayOfStrings Or use standard library functions. -JKop |
|
|
#6 |
|
|
Kapt. Boogschutter wrote:
> 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 p***ed 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 p*** POD types as variable arguments. Basically, better avoid variable argument lists. |
|
|
#7 |
|
|
Kapt. Boogschutter wrote:
> 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. -- edA-qa mort-ora-y (Producer) Trostlos Records <http://trostlos.org/> "What suffering would man know if not for his own?" |
|
|
#8 |
|
|
"Kapt. Boogschutter" <someone@nobody.com> schreef in bericht news:cb25ec$1cs$1@news4.tilbu1.nb.home.nl... > 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 p***ed 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 |