How to pass optional arguments to a method in C++?

Swapnil Gupta picture Swapnil Gupta · Sep 24, 2010 · Viewed 157.1k times · Source

How to pass optional arguments to a method in C++ ? Any code snippet...

Answer

Pramendra Gupta picture Pramendra Gupta · Sep 24, 2010

Here is an example of passing mode as optional parameter

void myfunc(int blah, int mode = 0)
{
    if (mode == 0)
        do_something();
     else
        do_something_else();
}

you can call myfunc in both ways and both are valid

myfunc(10);     // Mode will be set to default 0
myfunc(10, 1);  // Mode will be set to 1