In C++ programming language, Object-Oriented Programming (OOP) is very widely used as a way to work on data functions in a way that helps represent the real world in an abstract manner. Classes and Objects are the best way to work on properties and methods. In a modern C++ Compiler, one of the OOP features is copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain an implicitly-defined copy assignment operator in C++ with examples.
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 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.
1 2 3 4 5 6 7 |
class Tmyclass { public: std::string str; }; |
then we can create our objects with this Type of myclass
as shown below.
1 2 3 |
Tmyclass o1, o2; |
What is an implicitly-defined 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, struct, or union that is copy assignable (that you can copy with the = operator symbol), it has a default copy assignment operator. The implicitly-defined copy assignment operator is defined If neither deleted nor trivial. That means this operator has a function body which is generated and compiled. This is called as Implicitly-defined copy assignment operator.
In C++, T
represents a literal type, it can be function, class type (class
, struct
, union
object types), fundamentals type (void
, bool
, char
, wchar_t
), compound types (reference, pointer, array, function, enumeration).
Since C++11, if a class type has a user-declared destructor or user-declared copy constructor, the implicitly-defined copy assignment operator is deprecated. The implicitly-defined copy assignment operator for a class T
is constexpr
if,
- In general, for any non-static data member of type
T
- Since C++14, the Implicitly-defined copy assignment operator is used with a type
T
- Since C++23, the assignment operator selected to copy each direct base class subobject is a
constexpr
function - Since C++23, the implicitly-defined copy assignment operator for a class
T
isconstexpr
.
What are the declaration and definition of a class?
A declaration declares a unique name for the entity, along with information about its type (a type, class, struct, union) and other characteristics (parameters, options, base of it, etc.). In C++ all types, classes, structs, unions must be declared before they can be used.
A definition provides the compiler with all the information it needs to generate machine code when the entity is used later in the program. The definition means this operator has a function body which is generated and compiled.
Here is a simple syntax for default copy assignment operator with default option;
1 2 3 |
class_name & class_name :: operator= ( const class_name& ) = default; |
here is a declaration example in a class,
1 2 3 |
Tmyclass& operator=( const Tmyclass& other) = default; // Default Copy Assignment Operator |
here is a definition example including declaration,
1 2 3 4 5 6 |
Tmyclass& operator=( const Tmyclass& other) // declaration { // definition // definition } // definition |
now let’s see what is implicitly-defined copy assignment operator with a simple example.
Is there an example of an implicitly-defined copy assignment operator in C++?
After these useful information above, let’s give an example of an implicitly-defined copy assignment operator. Let’s assume that we use TmyclassA
as a base class and we have a new TmyclassB
class. This new class can use the copy assignment operator implicitly from the TmyclassA
.
Here is a TmyclassA
class example.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class TmyclassA { public: std::string str; TmyclassA& operator=(TmyclassA other) // Declaration of Copy Assignment Operator { // Definition of Copy Assignment Operator std::swap(str, other.str); // Definition of Copy Assignment Operator return *this; // Definition of Copy Assignment Operator } // Definition of Copy Assignment Operator }; |
and when you define a new TmyclassA
class example as below.
1 2 3 4 5 6 7 8 9 |
class TmyclassB : public TmyclassA { public: void print_str(){ std::cout <<str << std::endl; } // This class has implicitly-defined copy assignment }; |
As you see, here, because of : public TmyclassA
part, this TmyclassB
class has implicitly-defined copy assignment operator from TmyclassA
. Now we can use this in the copy of class objects as shown below.
1 2 3 4 5 |
TmyclassB o1, o2; o2 = o1; // Using Implicitly-defined Copy Assignment Operator |
Is there a full example of an implicitly defined copy assignment operator in C++?
Here is a full example with an implicitly-defined copy assignment operator in a class.
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 |
#include <iostream> #include <string> class TmyclassA { public: std::string str; TmyclassA& operator=(TmyclassA other) // Decleration of Copy Assignment Operator { // Definition of Copy Assignment Operator std::swap(str, other.str); // Definition of Copy Assignment Operator return *this; // Definition of Copy Assignment Operator } // Definition of Copy Assignment Operator }; class TmyclassB : public TmyclassA { public: void print_str(){ std::cout <<str << std::endl; } // This class has implicitly-defined copy assignment }; int main() { TmyclassB o1, o2; o1.str = "LearnCPlusPlus.org"; o2 = o1; // Using the implicitly-defined copy assignment of TMyclassB is used from TmyclassA o1.print_str(); o2.print_str(); system("pause"); return 0; } |
Here is the output.
1 2 3 4 5 |
LearnCplusplus.org LearnCplusplus.org Press any key to continue . . . |
As you see, in Modern C++, we can directly use an implicitly defined copy assignment operator from other classes in C++ without any definition.
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition