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

What Is A Forced (Default) Copy Assignment Operator In Modern C++

In the C++ programming language, Object-Oriented Programming (OOP) is a good way to represent data and organize the functionality of your code into logical groups. Classes and Objects are the best way to work on properties and methods. One of the features of OOP IDE is copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain the use of a forced default copy assignment operator with 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 a 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 or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the forced (defaulted) copy assignment operator with default option;

Syntax (Since C++11),

here is an example in a class:

What is a forced (default) copy assignment operator in C++?

The forced copy assignment operator forces a copy assignment operator to be generated by the compiler and it is default in any class declarations. This means you don’t need to declare it as above, let’s give examples without using it.

Let’s give a simple C++ example to copy assignment operator with default option, here is a simple class

This is same as below.

As you see both are same, because this is default in any class declaration and it is automatically declared.

And here is how you can use this “=” copy assignment operator on the objects of one of these given class examples,

Is there a full example to forced (default) 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 simply use the default copy assignment operator in C++ without any declaration. We can specialize the “=” operator what to copy or not, with the copy assignment operator.

What Is A Forced Default Copy Assignment Operator In Modern 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++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

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

Learn How To Use Types Of Destructors In C++?

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++?