Everything You Need To Know About The Copy Assignment Operator In C++ Classes
Classes and Objects are part of object-oriented methods and typically provide features such as properties and methods. One of the great features of an object orientated language like C++ is a copy assignment operator that is used with operator=
to create a new object from an existing one. In this post, we explain what a copy assignment operator is and its types in usage with some C++ examples.
What is a copy assignment operator in C++?
The Copy Assignment Operator in a class is a non-template non-static member function that is declared with the operator=
. When you create a class or a type that is copy assignable (that you can copy with the =
operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the typical declaration of a copy assignment operator which is defaulted:
Syntax (Since C++11).
class_name & class_name :: operator= ( const class_name& ) = default;
|
Here is an example in a class.
Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator
|
Is there a simple example of using the copy assignment operator in C++?
The forced copy assignment operator is default in any class declarations. This means you don’t need to declare it as above. Let’s give examples without using it.
Let’s give a simple C++ example to copy assignment operator with default
option, here is a simple class:
class myclass { public: std::string str;
};
|
Because this is default in any class declaration, and it is automatically declared. This class is same as below.
class myclass { public: std::string str;
Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator };
|
And here is how you can use this “=” copy assignment operator with both class examples above.
Tmyclass o1, o2;
o2 = o1; // Using Copy Assignment Operator
|
now let’s see different usage types in C++,
1. Typical Declaration of A Copy Assignment Operator with Swap
2. Typical Declaration of A Copy Assignment Operator ( No Swap)
3. Forced Copy Assignment Operator
4. Avoiding Implicit Copy Assignment
5. Implicitly-declared copy assignment operator
6. Deleted implicitly declared copy assignment operator
7. Trivial copy assignment operator
8. Eligible copy assignment operator
9. Implicitly-defined copy assignment operator
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.