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++.
Table of Contents
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Tuser { public: std::string name; } main() { Tuser user1, user2; user2 = user1; } |
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 (=),
1 2 3 4 5 |
int x1 = 10, x2; x2 = x1; // copy x1 value to x2 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Tuser { private: std::string name; public: Tuser( std::string s) // constructor { name = s; // set private name from a given string } // method to print name void print_name() { std::cout << name << std::endl; } } |
Normally, when you create a datatype such as a class it has a default assignment operator. So, we can use it as shown below.
1 2 3 |
user2 = user1; // using assignment operator |
Here is a full example about it,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <string> class Tuser { private: std::string name; public: Tuser( std::string s) // constructor { name = s; // set private name from a given string } // method to print name void print_name() { std::cout << name << std::endl; } } main() { Tuser user1("Yilmaz"); Tuser user2("Ata"); user2 = user1; // using assignment operator std::cout << "Names after using assignment operator:" << std::endl; user1.print_name(); user2.print_name(); } |
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 overloading, overloading 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:
1 2 3 4 5 6 7 |
// assignment operator overloading void operator=(const Tuser& U) { name = "Copy name of "+U.name; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <iostream> #include <string> class Tuser { private: std::string name; public: Tuser( std::string s) // constructor { name = s; // set private name from a given string } // assignment operator overloading void operator=(const Tuser& U) { name = "Copy name of "+U.name; } // method to print name void print_name() { std::cout << name << std::endl; } }; int main() { // Assigning by overloading constructor Tuser user1("Yilmaz"); Tuser user2("Ata"); std::cout << "Current Names:" << std::endl; user1.print_name(); user2.print_name(); // Using overloading assignment operator (=) user2 = user1; std::cout << std::endl<< "Names after using overloading assignment operator:" << std::endl; user1.print_name(); user2.print_name(); system("pause"); return 0; } |
and the output will be:
1 2 3 4 5 6 7 8 9 |
Current Names: Yilmaz Ata Names after using overloading assignment operator: Yilmaz Copy name of Yilmaz |
As you see, in modern C++, we can easily overload the assignment operator.
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition