Assignment Operators in C++ are the symbols that are used to assign the values to the variables. They perform simple value assignments and also combine with arithmetic and bitwise operators. In this article, we will discuss what an assignment operator in C++ is, its types with examples in C++, chaining assignment operators, limitations, and best practices.
Table of Contents:
What is an Assignment Operator in C++?
An assignment operator in C++ is used to assign values to a variable. It stores the right-hand value into the left-hand variable.
Syntax:
variable = value;
Example:
int a;
a = 10; // Assigns the value 10 to variable a
Types of Assignment Operators in C++
There is a basic, simple assignment operator, and also various compound assignment operators.
1. Simple Assignment Operator (=)
The simple assignment operator in C++ directly assigns the right-hand value to the left-hand variable.
Example:
Output:
The code shows how the simple assignment operator is used to assign the value 10 to the variable a and the value 20 to the variable b, and then the assigned variables are printed as a result to the console.
2. Compound Assignment Operators
Compound assignment operators are a combination of basic arithmetic and bitwise operators with the assignment operator.
Syntax:
variable op= expression;
Here is the list of all the compound assignment operators in C++:
Operator Name | Symbol | Syntax | Description |
Add and assign | += | a += b; | Adds b to a and stores the result in a |
Subtract and assign | -= | a -= b; | Subtracts b from a and stores the result |
Multiply and assign | *= | a *= b; | Multiplies a by b and updates a |
Divide and assign | /= | a /= b; | Divides a by b and updates a |
Modulo and assign | %= | a %= b; | Computes a % b and stores the result in a |
Bitwise AND and assign | &= | a &= b; | Performs bitwise AND on a and b, updates a |
Bitwise OR and assign | | | |= | Performs bitwise OR on a and b, updates a |
Bitwise XOR and assign | ^= | a ^= b; | Performs bitwise XOR on a and b, updates a |
Left shift and assign | <<= | a <<= b; | Shifts a left by b bits and updates a |
Right shift and assign | >>= | a >>= b; | Shifts a right by b bits and updates a |
Example:
Output:
The code shows how the various compound assignment operators are used to perform basic mathematical operations and then to update a value in the same variable.
Chaining Assignment Operator in C++
Chaining the assignment operator in C++ is referred to as assigning the same value to multiple variables in a single statement using the assignment operator(=). It works because the assignment operator gives a reference to the left-hand variable and allows further assignments.
Syntax:
a = b = c = value;
Example:
Output:
The code shows how the value 100 is first assigned to the variable c, then assigned to the variable b, and finally assigned to the variable a using chained assignment, which results in all three variables having the same value.
Assignment Between Different Classes in C++
Assignment between different classes in C++ is only possible if the following conditions are met:
- There must be a compatible conversion function.
- An appropriate assignment operator is overloaded.
Case 1: Using Conversion Constructor
In this approach, class B has a constructor that accepts an object of class A, and when an object of type A is assigned to an object of type B, then the compiler automatically calls this constructor to convert A into B.
Example:
Output:
The code shows that class B has a conversion constructor, which helps class B to be initialized from an object of class A by enabling the assignment B b = a, and then the assigned value is printed as an output.
Case 2: Overloading Assignment Operator
In this approach, class B overloads the assignment operator to accept an object of class A. It helps an existing object of class B to be assigned with values from an object of class A by using the syntax b = a;, with the custom logic defined in the operator function.
Example:
Output:
The code shows how class B overloads the assignment operator to accept an object of class A, which allows the assignment b = a; to copy the data from class A into class B.
Limitations of the Assignment Operator in C++
- Assignment operators are not inherited by derived classes in C++ and must be explicitly defined.
- The default assignment operator performs a shallow copy, which can lead to issues with pointers and dynamic memory.
- If the self-assignment is handled improperly, it can cause bugs or resource conflicts.
- The assignment operator must be manually overloaded for deep copying.
- An assignment between incompatible types is not allowed unless it is explicitly defined.
- Classes with the const or reference members cannot reassign those members in the assignment operator.
- If certain special member functions are defined, then the compiler may not generate a default assignment operator.
Best Practices for Using the Assignment Operator in C++
- Always check for self-assignment inside overloaded assignment operators.
- You must return *this by reference to allow chained assignments.
- Always mark the assignment operator as =, and delete if your class is unable to be copied.
- You should use the copy-and-swap idiom to write safer and exception-safe assignment logic.
- You must implement a copy constructor if you have to implement a copy assignment.
- Always keep the assignment operator consistent with the copy and move semantics of the class.
- You should use the const references in the parameter to avoid unnecessary copying.
- You must ensure exception safety to prevent resource leaks during partial assignments.
Conclusion
The assignment operator in C++ is an important operator to store values in variables. It also supports chaining and assignment between classes in C++. There are a variety of assignments that store values efficiently, but also have some limitations. So, by understanding the assignment operator and its types, limitations, and best practices, you can write efficient and maintainable C++ code.
Assignment Operators in C++ – FAQs
Q1. What is the assignment operator in C++?
The assignment operator is the operator that assigns the right-hand value to the left-hand variable using =.
Q2. What is a chained assignment?
Assigning the same value to multiple variables in one statement is called a chained assignment.
Q3. Can we assign between different classes?
Yes, we can assign between different classes using a conversion constructor and an overloaded assignment operator.
Q4. What is the risk of default assignment?
The default assignment performs a shallow copy, which can lead to issues with dynamic memory.
Q5. Why do I have to check for self-assignment?
You have to check for self-assignment to avoid bugs and resource mishandling when an object is assigned to itself.