In C++ programming language, Object-Oriented Programming (OOP) is a good way to represent and manipulate data, and work with functions in memory. Classes and Objects are the best way to work on properties and methods. In a modern C++ Compiler, one of the OOP features is the copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain why we mean by an implicitly-declared copy assignment operator in C++ along with some 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 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,
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 below,
1 2 3 |
Tmyclass o1, o2; |
What is an implicitly-declared 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. In modern C++, the compiler declares an inline copy assignment operator as a public member of the class. This is called as Implicitly-declared copy assignment operator. This operator may have two different forms, with const T&
and T&
as below,
1 2 3 |
T& T::operator=(const T&) |
and,
1 2 3 |
T& T::operator=(T&) |
These may depends on the parameters used in assignment operator. Note that a class may have multiple copy assignment operators. We can force the generation of the implicitly declared copy assignment operator with the default
keyword, if there are any defined copy assignment operators.
What is 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 the implicitly-declared copy assignment operator, which is also 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-declared copy assignment operator with a simple example.
Is there an example to implicitly-declared copy assignment operator in C++?
Let’s give a simple C++ example to implicitly-declared copy assignment operator which is copy assignment operator with default
option. Let’s assume that we have 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 |
class TmyclassA { public: std::string str; TmyclassA& operator=( const TmyclassA& other) = default; // Declaration of a Copy Assignment Operator }; |
and we can define a new TmyclassB
class 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-declared copy assignment }; |
As you see, here, because of the public TmyclassA
part, this TmyclassB
class has an implicitly-declared 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-declared Copy Assignment Operator |
1 2 3 4 5 6 7 8 9 10 |
class TmyclassA { public: std::string str; TmyclassA& operator=( const TmyclassA& other) = default; }; |
this class is the same as the one below:
1 2 3 4 5 6 7 8 9 10 |
class Tmyclass { public: std::string str; Tmyclass& operator=( const Tmyclass& other) = default; // Default Copy Assignment Operator }; |
As you see both are same, because this is the default in any class declaration, and because it is automatically declared it is called as implicitly-declared copy assignment operator. Here is how you can use an implicitly-declared copy assignment operator “=” on the objects in the first example.
1 2 3 4 5 |
Tmyclass o1, o2; o2 = o1; // Using Copy Assignment Operator |
Is there a full example of an implicitly declared copy assignment operator in C++?
An example with a 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 |
#include <iostream> #include <string> class TmyclassA { public: std::string str; TmyclassA& operator=( const TmyclassA& other) = default; // Declaration of a Copy Assignment Operator }; class TmyclassB : public TmyclassA { public: void print_str(){ std::cout <<str << std::endl; } // This class has implicitly-declared copy assignment }; int main() { TmyclassB o1, o2; o1.str = "LearnCPlusPlus.org"; o2 = o1; // Using the implicitly-declared 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 declared copy assignment operator in C++ without loss in efficiency. This implicitly declared copy assignment operator is always declared for any class. Note that the base class assignment operator is always hidden. The implicitly declared copy assignment operator of the derived class is the same as base class, and this operator declaration is also hidden.
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.