Object-Oriented Programming (OOP) is a method of mapping real-world objects and data to computer functions and data structures. Classes and Objects are part of object-oriented methods and typically provide features such as properties and methods. In Modern C++, one of the features of OOP is copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain what we mean by “avoiding implicit copy assignment operator”, and how can we use the delete option with copy assignment in C++ examples.
Table of Contents
What are classes and objects in C++?
Classes are defined in C++ using the 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. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented 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 copy assignment operator with delete option in C++?
The Copy Assignment Operator in a class is a non-template non-static member function that is declared with the “operator=“. Normally, a copy assignment operator is assigned in any class deceleration as default. For example, we can copy two object properties as below;
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Tmyclass { public: std::string str; }; Tmyclass o1, o2; o1.str = "LearnCPlusPlus.org"; o2 = o1; |
When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), sometime you don’t want it to be copied with other external code lines. In this situation, you need to use copy assignment operator with delete option. Here is a simple syntax for the copy assignment operator with delete
option;
Syntax (Since C++11),
1 2 3 |
class_name & class_name :: operator= ( const class_name& ) = delete; |
Here is an example in a class:
1 2 3 |
Tmyclass& operator=( const Tmyclass& other) = delete; // Deleting Copy Assignment Operator |
What is avoiding implicit copy assignment in C++?
In C++, the Copy Assignment operator is default in any class declaration and it is automatically declared. This is also called as the forced copy assignment operator which is default in any class declarations. This means, if you don’t want this default feature, you should delete by using delete
option as given syntax above.
Let’s give a simple C++ example of the copy assignment operator with default
option, here is a simple class:
1 2 3 4 5 6 7 |
class Tmyclass { public: std::string str; }; |
In modern C++, this simple class has hidden copy assignment operator as default
that is created automatically, this class example is same as below:
1 2 3 4 5 6 7 8 9 10 |
class Tmyclass { public: std::string str; Tmyclass& operator=( const Tmyclass& other) = default; // Copy Assignment Operator }; |
As you see both are same, and if you want to delete this copy operator to avoid implicit copy assignment usage, you need to use delete
option as below.
1 2 3 4 5 6 7 8 9 10 |
class Tmyclass { public: std::string str; Tmyclass& operator=( const Tmyclass& other) = delete; // Deleted Copy Assignment Operator }; |
And here is how you can use this “=” copy assignment operator on the objects of one of these given class examples:
1 2 3 4 5 |
Tmyclass o1, o2; o2 = o1; // Using Copy Assingment Operator |
Is there a full example to avoiding implicit copy assignment in C++?
Here is 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 |
#include <iostream> #include <string> class Tmyclass { public: std::string str; Tmyclass& operator=( const Tmyclass& other) = delete; // Deleting Default Copy Assignment Operator }; int main() { Tmyclass o1, o2; o1.str = "LearnCplusplus.org"; o2 = o1; // ERROR: We can not use Default Copy Assignment Operator, it is deleted. } |
As you see we successfully deleted the copy assignment operator in this class, thus if this is a class used in another source, we do not allow it be copied accidentally.
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