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

What is Implicitly-declared Copy Assignment Operator In C++?

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.

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,

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

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,

and,

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.

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;

here is a declaration example in a class,

Here is a definition example including declaration.

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.

and we can define a new TmyclassB class as below.

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.

this class is the same as the one below:

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.

Is there a full example of an implicitly declared copy assignment operator in C++?

An example with a copy assignment operator in a class,

here is the output:

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.

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

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?