Search code examples
c++optional-arguments

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


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


Solution

  • 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