I have a constructor which looks like:
RandomClass(const int16_t var1, const SecondClass& var2);
I need to pass a default argument to the second parameter, so currently I do something like this:
RandomClass(const int16_t var1, const SecondClass& var2 = *(new SecondClass(*(new std::unordered_map<int16_t, double>())));
which is incredibly awkward. Note that I do not want to use an overloaded constructor, or change the second parameter from a reference to a pointer.
What would an elegant way of passing the default parameter be?
You are allowed to bind a const reference to a temporary:
Of course this assumes that you either copy
var2into a member or do not use it after the constructor exits. If this is not the case, then something is wrong in your design. Perhaps using a raw pointer or ashared/weak_ptris more appropriate.