c++ - default value of const reference parameter -
currently have 2 functions:
void foo(const & a) { ... result = ... result += handle(a); // 1 bar(result); } void foo() { ... result = ... bar(result); }
all code in foo() same except 1
.
can merge them 1 function following?
void foo(const & = 0) { ... ... if (a) result += handle(a); // won't work, can similar? bar(result); }
btw, parameter has reference keep interface unchanged.
you can use null object pattern.
namespace { const null_a; // (possibly "extern") } void foo(const & = null_a) { ... result = ... if (&a != &null_a) result += handle(a); bar(result); }
Comments
Post a Comment