• C++ Data Types
  • C++ Input/Output
  • C++ Pointers
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management

Self assignment check in assignment operator

In C++, assignment operator should be overloaded with self assignment check.

For example, consider the following class Array and overloaded assignment operator function without self assignment check.

If we have an object say a1 of type Array and if we have a line like a1 = a1 somewhere, the program results in unpredictable behavior because there is no self assignment check in the above code. To avoid the above issue, self assignment check must be there while overloading assignment operator. For example, following code does self assignment check.

References: http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html

author

Similar Reads

  • Self assignment check in assignment operator In C++, assignment operator should be overloaded with self assignment check. For example, consider the following class Array and overloaded assignment operator function without self assignment check. // A sample class class Array { private: int *ptr; int size; public: Array& operator = (const Ar 2 min read
  • Assignment Operators In C++ In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign 7 min read
  • Is assignment operator inherited? In C++, like other functions, assignment operator function is inherited in derived class. For example, in the following program, base class assignment operator function can be accessed using the derived class object. #include<iostream> using namespace std; class A { public: A & operator= ( 1 min read
  • Default Assignment Operator and References in C++ We have discussed assignment operator overloading for dynamically allocated resources here. In this article, we discussed that when we don't write our own assignment operator, the compiler creates an assignment operator itself that does shallow copy and thus causes problems. The difference between s 2 min read
  • Move Assignment Operator in C++ 11 In C++ programming, we have a feature called the move assignment operator, which was introduced in C++11. It helps us handle objects more efficiently, especially when it comes to managing resources like memory. In this article, we will discuss move assignment operators, when they are useful and call 5 min read
  • Copy Constructor vs Assignment Operator in C++ Copy constructor and Assignment operator are similar as they are both used to initialize one object using another object. But, there are some basic differences between them: Copy constructor Assignment operator It is called when a new object is created from an existing object, as a copy of the exist 2 min read
  • C++ Assignment Operator Overloading Prerequisite: Operator Overloading The assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types. Assignment operator overloading is binary operator overloading.Overloading a 4 min read
  • array::operator[ ] in C++ STL Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator. 2 min read
  • When should we write our own assignment operator in C++? The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment operato 3 min read
  • set operator= in C++ STL The ‘=’ is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set 2 min read
  • Increment (Decrement) operators require L-value Expression What will be the output of the following program? #include<stdio.h> int main() { int i = 10; printf("%d", ++(-i)); return 0; } A) 11 B) 10 C) -9 D) None Answer: D, None - Compilation Error. Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) operato 1 min read
  • std::is_nothrow_move_assignable in C++ The std::is_nothrow_move_assignable template of C++ STL is present in the <type_traits> header file. The std::is_nothrow_move_assignable template of C++ STL is used to check whether the T is a move assignable type or not and this is known for not to throw any exception. It return the boolean v 2 min read
  • multimap::operator= in C++ STL multimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. Syntax:- multimap1 = (multimap2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as p 2 min read
  • list assign() function in C++ STL The list::assign() is a built-in function in C++ STL which is used to assign values to a list. It can also be used to copy elements from one list to another. To assign elements to a list. Syntax: list_name.assign(count, value) Parameters: This function accepts two mandatory parameters as shown in th 2 min read
  • Conditionally assign a value without using conditional and arithmetic operators Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked: If 'x' is 0, Assign value 'a' to variable 'y' Else (If 'x' is 1) Assign value 'b' to variable 'y'. Note: - You are not allowed to use any conditional operator (including the ternary 6 min read
  • unordered_multiset operator = in C++ STL The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_multiset to another unordered_multiset and unordered_multiset::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an unordered_multiset as an argument 2 min read
  • How to Overload == Operator in C++? A class in C++ is the building block that leads to Object-Oriented programming. Class is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. The overloading of operators is a polymorphism that occurs a 2 min read
  • C/C++ Ternary Operator - Some Interesting Observations Predict the output of following C++ program. #include <iostream> using namespace std; int main() { int test = 0; cout << "First  character " << '1' << endl; cout << "Second character " << (test ? 3 : '1') << endl; return 0; } One would ex 3 min read
  • Vector Operator = in C++ STL In C++, the vector operator = is used to assign the contents of one vector to another. It allows you to copy elements from one vector to another or initialize a vector with another vector's contents. Let’s take a look at a simple code example: [GFGTABS] C++ #include <bits/stdc++.h> using names 3 min read

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. C++ String コピー

    c self assignment copy constructor

  2. Copy Constructor in C++

    c self assignment copy constructor

  3. Copy Constructor in C++: Syntax, Types, and Examples

    c self assignment copy constructor

  4. Copy Constructor in C#

    c self assignment copy constructor

  5. Copy Constructor in C# Class

    c self assignment copy constructor

  6. Copy Constructor in C++ with Example Program

    c self assignment copy constructor

VIDEO

  1. C++: การเขียน copy constructor และ assignment operator (ต่อจาก Rule of Three)

  2. C++ shallow copy constructor vs deep copy constructor

  3. Types of Constructors in C++

  4. 🥳10th class self assignment social studies viral question paper answer key 2024-25ll

  5. Assignment Operator Overloading In C++

  6. C++ Constructors: Types and Copy Constructors || Constructors in OOP || #codewithredoy