I have a function of type
virtual void foo(bla, bla, bla, std::shared_ptr<LoggerInterface> logger) = 0;
And I want to pass a default parameter with NULL pointer, something like:
virtual void foo(bla, bla, bla, std::shared_ptr<LoggerInterface> logger = NULL) = 0;
So that in implementation, if logger is NULL
I do nothing with it, otherwise I use the logger.
I've tried to look for a solution but cannot find..
UPD: Duplicate claim is irrelevant, I am asking about default NULL parameter.
Is it possible that gcc 4.4 doesn't support nullptr?
With a C++11 compliant compiler
This should work with either
NULL
ornullptr
the latter being the recommended form for C++. The only condition is that you compile it for c++11 or higher (see comment of jamek about the command line options for gcc).See online demo
Important remark about default parameters
Note that the default parameter is not intrinsic to the function itself, but to the context in which the default value was declared. In the example above:
b->foo(15)
works, because the function is accessed using theB
class for which a default parameter is defined.d.foo(15)
won't even compile, despite both referring to the same function. This is because for theD
class I didn't declare a default value in the definition of the override.Limitated implementation of C++11 in GCC 4.4
Indeed,
nullptr
was introduced with GCC 4.6 and you have to wait GCC 4.8.1 for a full C++11 implementation.In fact GCC 4.4.0 was released in 2009 and the first sub-release after the official approval of the standard in august 2011 was GCC 4.4.7.