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++.
Table of Contents
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,
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),
here is an example in a class:
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.
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.
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++.
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.