Let's say I have class with N members. Most member are copyable. Only one member needs manual copying code.
Is there method to write copy assignment operator in such a way that I write code only for nonstandard member, and letting compiler generate copying code for all/other members ?
Answer: A constructor can be defined as a special member function which is used to initialize the objects of the class with initial values. It is special member function as its name is the same as the class name. It enables an object to initialize itself during its creation.
You can safely invoke the copy assignment operator from the constructor as long as the operator is not declared virtual.
Assignment Operators Overloading in C++You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor. Following example explains how an assignment operator can be overloaded.
Assignment Operators in C/C++ Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.
Yes, of course, wrap your member in a separate class with a user-defined copy-constructor. And for the class you are talking about, write no user-defined copy constructor.
E.g.
class MyMemWrapper
{
define copy constructor
};
class MyClass
{
member 1;
member 2;
...
MyMemWrapper
};
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