What Is An Eligible Copy Assignment Operator In C++?
In a modern C++ IDE, one of the features of its modern is the copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain an eligible copy assignment operator in C++.
What are classes and objects in C++?
Classes are defined in C++ using keyword class
followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program, and they work as an object constructor. Objects are an instantiation of a class, In another term. In C++ programming, most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below,
class Tmyclass { public: std::string str; };
|
then we can create our objects with this Type of myclass as below,
What is 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 forced (defaulted) copy assignment operator with default option;
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; // Default Copy Assignment Operator
|
This default copy assignment operator is declared automatically in a new class declaration, it is implicitly-defined or defaulted copy assignment operator and also a trivial copy assignment operator.
What is an eligible copy assignment operator in C++?
Before the C++11 standard, a copy assignment operator was ‘eligible’ when the copy assignment operator is either user-declared or both implicitly declared and definable. Since C++11 (and until C++20), copy assignment operator is generated automatically and the Eligible Copy Assignment Operator is a copy assignment operator that is eligible if this default operator or user defined copy assignment operator is not deleted.
Since C++20, a copy assignment operator in C++ is eligible:
- if it is not deleted
- if it has associated constraints, they are satisfied
- if there is no more constrained than this operator with the same first parameter type and the same cv/ref-qualifiers
Is there a simple example of an eligible copy assignment operator in C++?
Let’s give an example of an eligible copy assignment operator. Let’s assume that we have TmyclassA
as a base class and we have a new TmyclassB
class using TmyclassA
as a base class.
class TmyclassA { public: std::string str; };
class TmyclassB : public TmyclassA { // This class has Eligible Copy Assignment Operator from base };
|
Here, In C++11, C++14, C++17 standards, TmyclassB
has eligible copy assignment operator from TmyclassA
base. Because it is not deleted from the base. Now, let’s assume that we have TmyclassC
as a base class and has deleted copy assignment operator, and we have a new TmyclassD
class using TmyclassC
as a base class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class TmyclassC { public: std::string str;
TmyclassC& operator=( const TmyclassC& other) = delete; };
class TmyclassD : public TmyclassC { // This class has no Eligible Copy Assignment Operator from base, because it is deleted public: TmyclassD& operator=( const TmyclassD& other) // User Defined Copy Assignment Operator { str = other.str; } };
|
Here, TmyclassD
has no eligible copy assignment operator from TmyclassC
base, because default copy assignment operator is deleted in TmyclassC
definition. Thus, user defined his own Copy Assignment Operator in TmyclassD
.
Is there a full example of an eligible copy assignment operator in C++?
If we combine both examples above, here is a full example of an eligible copy assignment operator in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include #include
class TmyclassA { public: std::string str; };
class TmyclassB : public TmyclassA { // This class has Eligible Copy Assignment Operator from base };
class TmyclassC { public: std::string str;
TmyclassC& operator=( const TmyclassC& other) = delete; };
class TmyclassD : public TmyclassC { // This class has no Eligible Copy Assignment Operator from base, because it is deleted public: TmyclassD& operator=( const TmyclassD& other) // User Defined Copy Assignment Operator { str = other.str; } };
int main() { TmyclassB o1, o2; o1.str = “LearnCplusplus.org”;
o2 = o1; // Using Eligible Copy Assignment Operator
std::cout << o1.str << std::endl; std::cout << o2.str << std::endl;
TmyclassD o3, o4; o3.str = “I Love C++”; o4 = o3; // Using User Defined Copy Assignment Operator
std::cout << o3.str << std::endl; std::cout << o4.str << std::endl;
system(“pause”); return 0; }
|
As you see, in Modern C++, each new class has copy assignment operator and they are eligible in other class declarations when they have this base.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. 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 cross-platform 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 version.