Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloading copy constructor

13 views
Skip to first unread message

bitrex

unread,
Nov 21, 2016, 3:37:53 PM11/21/16
to
How can I do something like this with a copy constructor where I'd like
a second default argument, where the default is a member variable of the
class the copy is being made from

MyClass(const MyClass& my_class, member=my_class._member) :
_member(member) { etc...

Currently I'm getting a "local variable ‘my_class’ may not appear in
this context" error as it isn't in the appropriate scope for a default
argument.

Marcel Mueller

unread,
Nov 21, 2016, 4:06:42 PM11/21/16
to
You need to overload the constructor.

MyClass(const MyClass& my_class) : _member(my_class._member) ...
MyClass(const MyClass& my_class, member) : _member(member) ...


Default members are no overloads. They are just default arguments that
are passed by the caller when no argument is given.
This might look similar to overloading but it is entirely different. If
you change a default parameter you need to recompile every caller. If
you change the overloaded implementation (without the additional
parameter) to assign another default value you need to recompile the
callee only.


Marcel
0 new messages