PDA

Click to See Complete Forum and Search --> : optional parameters


Sam Finch
Jun 2nd, 2000, 02:34 AM
can you use optional parameters in C++, like in VB


Function MyFunc(Optional Pram As Long = 10) As Long

how would I do that in C++ without overloading, I want to do this for a constructor so I can't call it from overloaded versions, and I have loads of optional params of the same type anyway so It'd make it hard to overload

parksie
Jun 3rd, 2000, 08:57 PM
You can use the stdarg system. I'm not too sure how it works, but you basically define your function thus:


void myfunc(int arg_a, bool arg_b, ...) {

}


This means that arg_a and arg_b are compulsory (in the normal way), but other arguments can be added onto the end.

there is then a set of functions and macros to extract an array of parameters. Take printf for example - if you can get the source code to that you should be fine.

Shabble
Jun 4th, 2000, 11:40 AM
look up va_arg, va_start or va_end in the help files - MSVC has an example program there to help, I'd imagine *nix would have similar in their man pages.