Reputation: 1521
I want to use a constructor parameter some_function
as parameter to a default initialization of some other constructor parameter, namely some_other_function
:
SomeConstructor(SomeFunction some_function,
SomeOtherFunction some_other_function = SomeOtherFunction(some_function)) :
some_function_(some_function),
some_other_function_(some_other_function)
{...}
Unfortunately, this gives a compile error:
error: 'some_function' was not declared in this scope
Upvotes: 2
Views: 138
Reputation: 16156
Just do it without the syntactic sugar(*):
Constructor (A a, B b)
: a_(a), b_(b) {
// ...
}
Constructor (A a)
: Constructor (a, B(a)) {}
Though this only works since C++11, the feature is known as delegating or forwarding constructors.
(*) "syntactic sugar" isn't really the correct term here, as it's not semantically equivalent to the constructor with the default argument. (Default parameters are - as far as I know - inserted as "normal" parameters at the call site.)
Upvotes: 6