C++C++11C++14C++17C++20Learn C++Syntax

What Is Deleted Implicitly-declared Copy Assignment Operator In C++?

In the C++ programming language, Object-Oriented Programming (OOP) is a good way to represent and manipulate data and work with functions. Classes and Objects are the best way to work on properties and methods. In a professional 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 what the Deleted Implicitly-declared copy assignment operator in C++ examples.

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,

then we can create our objects with this Type of myclass as below,

What is deleted 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.

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. This operator can be deleted, then this is called a deleted implicitly-defined copy assignment operator or we say that the implicitly-defined copy assignment operator is deleted.

How does a deleted implicitly-declared copy assignment operator occur in C++?

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).

Normally, a copy assignment operator is defined as default in any new type T definition. We can list these below for the deleted implicitly-defined copy assignment operator,

  • If type T has a user-declared move constructor, implicitly-declared copy assignment operator for class type T is defined as deleted
  • If type T has a user-declared move assignment operator, an implicitly-declared copy assignment operator for class type T is defined as 
  • If type T has a base class that has deleted copy assignment operator, implicitly-declared copy assignment operator for class type T is defined as deleted

A copy assignment operator is defined as default in any new type T definition.

A copy assignment operator is defined as default in any new type T definition. If you just need default copy assignment operator, you don’t need to declare anything about it, it is automatically declared and defined. Here is a simple example below,

If type T has a user-declared move constructor, implicitly-declared copy assignment operator for class type T is defined as deleted

If type T has a user-declared move constructor, implicitly-declared copy assignment operator for class type T is defined as deleted. Let’s give an example to this. Assume that we have a class with user-defined implicitly-declared copy assignment operator which is empty (no operation) as given example below,

If we run this example you will see that implicitly-declared copy assignment operator (which is default) is deleted and our new empty assignment operator definition is running. This is why second line is empty (Normally, you will see two text lines of “LearnCplusplus.org”)

If type T has a user-declared move assignment operator, an implicitly-declared copy assignment operator for class type T is defined as deleted 

If type T has a user-declared move assignment operator, an implicitly-declared copy assignment operator for class type T is defined as deleted.  Let’s give an example of this. Assume that we have a class with user-defined implicitly-declared copy assignment operator:

This will delete the implicitly-defined copy assignment operator. If we compile this example, we will see an error as shown below.

If type T has a base class that has deleted copy assignment operator, implicitly-declared copy assignment operator for class type T is defined as deleted

If type T has a base that has deleted copy assignment operator, f type T has a deleted copy assignment operator for class type T is defined as deleted.  Let’s give an example of this. Assume that we have a TmyclassA base class that has deleted copy assignment operator, and if we use this base class in another class TmyclassB as below:

if we compile this example, using copy assignment operator will give this error.

What is more about deleted implicitly-defined copy assignment operator in C++?

In addition to these, a defaulted copy assignment operator for class T is defined as deleted If type T has one of a non-static data member of :

  • A const-qualified non-class type.
  • A reference type.
  • A non-static data member that cannot be copy-assigned.
  • A direct base class that cannot be copy-assigned.
  • A variant member that has non-trivial assignment operator.

In Modern C++, as you see, we may cause to deletion of implicitly-declared copy assignment operator for class type in C++, as listed in these conditions above.

What Is Deleted Implicitly declared Copy Assignment Operator In C++C++ Builder logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++