This is a quick tip in C++ for providing support for optional parameters on functions. This works because when a function is declared in C++ it can be given a default value and when that parameter is missing from the calling function the compiler will automatically add it during compile time and pass the default value.

Here is a simple example of this.

#include <stdio.h>

bool func(bool val = false) {
    return val;
}


int main(int argc, char **argv) {

    printf("%d\n", func());
    printf("%d\n", func(false));
    printf("%d\n", func(true));

    return 0;
}




Last Modified: 14 December 2016

Releated Posts


2013-08-22 - CPP - Prevent an object by being copied with boost::noncopyable
2013-03-29 - CPP - Why you need a copy constructor
2013-03-27 - CPP -boost::optional
2012-11-06 - C++ - Stringstream example
2012-08-09 - C++ - Check an IP Address is in a IP Mask
2012-06-30 - CPP - Creating A basic python wrapper
2012-06-16 - CPP - Using gperf
2012-05-10 - CPP - Optional function arguments
2012-03-19 - CPP - Read / Write std::map to a file.