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

What Is An Implicitly-defined Copy Assignment Operator In C++?

What Is An Implicitly defined Copy Assignment Operator In C++

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.

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 shown below.

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 is constexpr.

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.

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;

here is a declaration example in a class,

here is a definition example including declaration,

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.

and when you define a new TmyclassA class example as below.

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.

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.

Here is the output.

As you see, in Modern C++, we can directly use an implicitly defined copy assignment operator from other classes in C++ without any definition.

What Is An Implicitly defined 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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond