Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write constructor assignment operator only mentioning specially -copied members?

Tags:

c++

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 ?

like image 715
Andrei Avatar asked Mar 19 '11 19:03

Andrei


People also ask

Why constructor is called special member function?

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.

Can you use the copy constructor in the assignment operator?

You can safely invoke the copy assignment operator from the constructor as long as the operator is not declared virtual.

Is assignment operator overloaded by default in C++?

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.

What is special assignment operator in C++?

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.


1 Answers

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
};
like image 147
Armen Tsirunyan Avatar answered Oct 07 '22 09:10

Armen Tsirunyan