C++C++11Learn C++

What Is Assignment Operator Overloading?

What Is Assignment Operator Overloading

One of the most commonly used features of C++ software, in common with many programming languages, is the “=” assignment operator. These take the form of copy assignment and move assignment operators. In C++, we can overload the “=” assignment operator by creating a new assignment operator, this is called assignment operator overloading. In this post, we explain what an assignment operator is, what overloading means, and how we can overload an assignment operator in C++.

What is assignment operator?

In C++, the “=” operator is the assignment operator, it is used for assigning values between two data types, such as int, string, class objects, struct objects, etc. By default, it copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types. Here is a simple example to use this assignment operator,

In example above, “=” assignment operator will copy name of user1 to user2.

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. If you are new to assignment operator, here we have more details for you:

Is there an example to default assignment operator?

Here is a simple usage of assignment operator (=),

Now, let’s see what happens with the default assignment operator when we use between class objects. Let’s assume that we have a Tuser class that has a name string variable in private. We can add a constructor to set this private name variable. We can also add a print_name() method to print this private name. Our Tuser class will be as follows:

Normally, when you create a datatype such as a class it has a default assignment operator. So, we can use it as shown below.

Here is a full example about it,

This example above is also good to show how you can set and print a private variable in a class.

What is overload, overloaded, or overloading means in programming ?

In modern programming, overload, overloaded, overloading, function overloading, or method overloading terms are used for the name of the concept of having more than one method with the same name but with different parameters. In other words, it means that your code is providing a method or function with the same name, but with a different operation, maybe with different variable types.

Method or Function Overloading is used to operate with the same function by defining it with different variable types. By using Method or Function Overloading, a function can be used with the same name in different parameter types and multiple variations. If you want to discover more about it, here it is:

Then, what is assignment operator overloading?

Assignment operator overloading means we overload the “=” assignment operator by creating a new assignment operator. For example, if we want to use the assignment operator “=” to assign the value of the class members in a different way of copying them to another class member, then we should redefine a new assignment operator.  This is called assignment operator overloadingoverloading assignment operator, or overloaded assignment operator.

How can we use the assignment operator overloading?

For example, we can redefine the copy assignment operator in a class. If we go on from the example above, in our Tuser class we can overload this default assignment operator. Here is how we can do this:

When we copy class objects, the new one will have “Copy name of ” prefix before the name.

Is there a full example about C++ assignment operator overloading?

Here is a full example of how to use C++ assignment operator overloading.

and the output will be:

As you see, in modern C++, we can easily overload the assignment operator.

What Is Assignment Operator Overloading 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++?