2

Is something like this allowed in C++? If so, how do I do it?

myFunction("User input: %s", a);

Or is the only way to format it first and then pass it?

thorvald
  • 217
  • 3
  • 10

5 Answers5

3

If you want to write a function that takes variable parameters, the answer is yes, and here's how you declare such a function:

void myFunction(const char*, ...);

Note the elipsis at the end.

But now that you know how to do this, I'll complicate things for you. You should not do this. The myFunction() declared above is type-unsafe, and completely sidesteps every means C++ has to protect you from mistakes.

There are other ways to accomplish something like this (vector comes to mind), but I have found that if you're needing a function that takes variable parameters, it's a code smell that indicates that there is somethign wrong with your design in the first place. Take a closer look at why you need this.

EDIT:

If what you're trying to do is not pass variable parameters per se, but a formatted string (like your OP says), then you would need to do the string formatting yourself in your function. There are ways to do this depending on your platform. In Windows, you'd probably use vsprintf() or something similar.

But again, if you need to do this, there are better ways. Personally I'm fond of using Boost format.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Yup -- sounds like a case for extract method object :P – Billy ONeal Nov 30 '10 at 15:57
  • Hm, I thought if the formatting was done at the part where the string is sent, the function receives it as a single parameter.. or declare the function to receive sprintf type. So looks like I'll format it first and then pass it. – thorvald Nov 30 '10 at 16:03
  • @thorvald: I;m not sure what you mean by "formatting was done at the part where the string is sent." Can you explain? – John Dibling Nov 30 '10 at 16:08
  • Also, `sprintf` isn't a type -- it's a function. – John Dibling Nov 30 '10 at 16:08
3

I guess what you want is to call a function like vsprintf from your myFunction - this way you can do the formatting inside of your function. Example:

void myFunction(const char *format, ...)
{
  char buf[1024];

  va_list arglist;
  va_start( arglist, format );
  vsprintf(buf,format,arglist);
  va_end( arglist );
}
Suma
  • 33,181
  • 16
  • 123
  • 191
  • See also http://stackoverflow.com/questions/41400/how-to-wrap-a-function-with-variable-length-arguments – Suma Dec 10 '10 at 13:18
1

It's legal to do what you're doing, except:

sprintf's behavior is to apply formatting to a string. Outside of that function (and the printf family in general), your arguments here don't really 'mean' anything, because your function signature could look like this:

void myFunction(const char* str1, const char* str2);

But what it does is up to you.

If you want a string modified before passing it into some function, you will either have to modify it outside myFunction (via sprintf or whatever) or pass all your arguments in and call sprintf inside the function.

wkl
  • 77,184
  • 16
  • 165
  • 176
0

Yes, it's allowed. No, it is not a part of the standard library, so you have to implement it yourself.

zvrba
  • 24,186
  • 3
  • 55
  • 65
0

It is possible using the vsprintf() function from the C library, and writing myFunction() as taking variable arguments.

However, the usual disclaimers regarding variable argument methods apply: it's very easy to introduce buffer overruns or segmentation violations this way. Some compilers allow you to declare myFunction() as "printf-like", so that you could get some degree of compile-time checking of the arguments, but that is not portable.

Lars
  • 1,377
  • 6
  • 6