In C++, how would I specify a default value for a list reference in a function?
void fun(
std::list<My_Object *> &the_list,
int n = 4
)
if it is a plain reference, the only thing you an default it to is a valid lvalue which probably is not available. But if it is a reference to const you could default it to an empty list like this:
void fun(
std::list<My_Object *> const & the_list = std::list<My_Object *>(),
int n = 4
)
If you have a list named a, which is available at the declaration site, then like this
void fun(
std::list<My_Object *> & the_list = a,
int n = 4
)
but be careful so that the a
list is still "alive" when you call the function
In C++, how would I specify a default value for a list reference in a function?
I wouldn't, in your case. Either overload the function so that it can be called without a list, or take the argument per pointer, so that users can pass a NULL
pointer.
I'd strongly prefer overloading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With