1 Salrajas

Self Assignment Check C Points

Customizes the C++ operators for operands of user-defined types.

[edit]Syntax

Overloaded operators are functions with special function names:

op (1)
type (2)

(3)

(4)
suffix-identifier (5) (since C++11)
op - any of the following 38(until C++20)39(since C++20) operators:+-*/%^&|~!=<>+=-=*=/=%=^=&=|=<<>>>>=<<===!=<=>=<=>(since C++20)&&||++--,->*->()[]

1) overloaded operator;

2)user-defined conversion function;

3)allocation function;

4)deallocation function;

5)user-defined literal.

[edit]Overloaded operators

When an operator appears in an expression, and at least one of its operands has a class type or an enumeration type, then overload resolution is used to determine the user-defined function to be called among all the functions whose signatures match the following:

Expression As member function As non-member function Example
@a (a).operator@ ( ) operator@ (a) !std::cin calls std::cin.operator!()
a@b (a).operator@ (b) operator@ (a, b) std::cout<<42 calls std::cout.operator<<(42)
a=b (a).operator= (b) cannot be non-member std::string s; s ="abc"; calls s.operator=("abc")
a(b...) (a).operator()(b...) cannot be non-member std::random_device r;auto n = r(); calls r.operator()()
a[b] (a).operator[](b) cannot be non-member std::map<int, int> m; m[1]=2; calls m.operator[](1)
a-> (a).operator-> ( ) cannot be non-member auto p =std::make_unique<S>(); p->bar() calls p.operator->()
a@ (a).operator@ (0) operator@ (a, 0) std::vector<int>::iterator i = v.begin(); i++ calls i.operator++(0)

in this table, is a placeholder representing all matching operators: all prefix operators in @a, all postfix operators other than -> in a@, all infix operators other than = in a@b

Note: for overloading user-defined conversion functions, user-defined literals, allocation and deallocation see their respective articles.

Overloaded operators (but not the built-in operators) can be called using function notation:

[edit]Restrictions

  • The operators (scope resolution), (member access), (member access through pointer to member), and (ternary conditional) cannot be overloaded.
  • New operators such as , , or cannot be created.
  • The overloads of operators and lose short-circuit evaluation.
  • The overload of operator must either return a raw pointer or return an object (by reference or by value), for which operator is in turn overloaded.
  • It is not possible to change the precedence, grouping, or number of operands of operators.
  • , , and (comma) lose their special sequencing properties when overloaded and behave like regular function calls even when they are used without function-call notation.
(until C++17)

[edit]Canonical implementations

Other than the restrictions above, the language puts no other constraints on what the overloaded operators do, or on the return type (it does not participate in overload resolution), but in general, overloaded operators are expected to behave as similar as possible to the built-in operators: operator+ is expected to add, rather than multiply its arguments, operator= is expected to assign, etc. The related operators are expected to behave similarly (operator+ and operator+= do the same addition-like operation). The return types are limited by the expressions in which the operator is expected to be used: for example, assignment operators return by reference to make it possible to write a = b = c = d, because the built-in operators allow that.

Commonly overloaded operators have the following typical, canonical forms:[1]

[edit]Assignment operator

The assignment operator (operator=) has special properties: see copy assignment and move assignment for details.

The canonical copy-assignment operator is expected to perform no action on self-assignment, and to return the lhs by reference:

The canonical move assignment is expected to leave the moved-from object in valid state (that is, a state with class invariants intact), and either do nothing or at least leave the object in a valid state on self-assignment, and return the lhs by reference to non-const, and be noexcept:

In those situations where copy assignment cannot benefit from resource reuse (it does not manage a heap-allocated array and does not have a (possibly transitive) member that does, such as a member std::vector or std::string), there is a popular convenient shorthand: the copy-and-swap assignment operator, which takes its parameter by value (thus working as both copy- and move-assignment depending on the value category of the argument), swaps with the parameter, and lets the destructor clean it up.

This form automatically provides strong exception guarantee, but prohibits resource reuse.

[edit]Stream extraction and insertion

The overloads of and that take a std::istream& or std::ostream& as the left hand argument are known as insertion and extraction operators. Since they take the user-defined type as the right argument ( in a@b), they must be implemented as non-members.

These operators are sometimes implemented as friend functions.

[edit]Function call operator

When a user-defined class overloads the function call operator, operator(), it becomes a type. Many standard algorithms, from std::sort to std::accumulate accept objects of such types to customize behavior. There are no particularly notable canonical forms of operator(), but to illustrate the usage

[edit]Increment and decrement

When the postfix increment and decrement appear in an expression, the corresponding user-defined function (operator++ or operator--) is called with an integer argument . Typically, it is implemented as T operator++(int), where the argument is ignored. The postfix increment and decrement operator is usually implemented in terms of the prefix version:

Although canonical form of pre-increment/pre-decrement returns a reference, as with any operator overload, the return type is user-defined; for example the overloads of these operators for std::atomic return by value.

[edit]Binary arithmetic operators

Binary operators are typically implemented as non-members to maintain symmetry (for example, when adding a complex number and an integer, if is a member function of the complex type, then only complex+integer would compile, and not integer+complex). Since for every binary arithmetic operator there exists a corresponding compound assignment operator, canonical forms of binary operators are implemented in terms of their compound assignments:

[edit]Relational operators

Standard algorithms such as std::sort and containers such as std::set expect operator< to be defined, by default, for the user-provided types, and expect it to implement strict weak ordering (thus satisfying the concept). An idiomatic way to implement strict weak ordering for a structure is to use lexicographical comparison provided by std::tie:

Typically, once operator< is provided, the other relational operators are implemented in terms of operator<.

Likewise, the inequality operator is typically implemented in terms of operator==:

When three-way comparison (such as std::memcmp or std::string::compare) is provided, all six relational operators may be expressed through that:

All six relational operators are automatically generated by the compiler if the three-way comparison operator operator<=> is defined, and that operator, in turn, is generated by the compiler if it is defined as defaulted:

See default comparisons for details.

struct Record {std::string name;unsignedint floor;double weight;auto operator<=>(const Record&)=default;};// records can now be compared with ==, !=, <, <=, >, and >=
(since C++20)

[edit]Array subscript operator

User-defined classes that provide array-like access that allows both reading and writing typically define two overloads for operator[]: const and non-const variants:

If the value type is known to be a built-in type, the const variant should return by value.

Where direct access to the elements of the container is not wanted or not possible or distinguishing between lvalue c[i]= v; and rvalue v = c[i]; usage, operator[] may return a proxy. see for example std::bitset::operator[].

To provide multidimensional array access semantics, e.g. to implement a 3D array access a[i][j][k]= x;, operator[] has to return a reference to a 2D plane, which has to have its own operator[] which returns a reference to a 1D row, which has to have operator[] which returns a reference to the element. To avoid this complexity, some libraries opt for overloading operator() instead, so that 3D access expressions have the Fortran-like syntax a(i, j, k)= x;

[edit]Bitwise arithmetic operators

User-defined classes and enumerations that implement the requirements of are required to overload the bitwise arithmetic operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^=, and may optionally overload the shift operators operator<<operator>>, operator>>=, and operator<<=. The canonical implementations usually follow the pattern for binary arithmetic operators described above.

[edit]Boolean negation operator

The operator operator! is commonly overloaded by the user-defined classes that are intended to be used in boolean contexts. Such classes also provide a user-defined conversion function explicit operator bool() (see std::basic_ios for the standard library example), and the expected behavior of operator! is to return the value opposite of operator bool.

[edit]Rarely overloaded operators

The following operators are rarely overloaded:

  • The address-of operator, operator&. If the unary & is applied to an lvalue of incomplete type and the complete type declares an overloaded operator&, the behavior is undefined(until C++11) it is implementation-defined whether the overloaded operator is used(since C++11). Because this operator may be overloaded, generic libraries use std::addressof to obtain addresses of objects of user-defined types. The best known example of a canonical overloaded operator& is the Microsoft class CComPtr. An example of its use in EDSL can be found in boost.spirit.
  • The boolean logic operators, operator&& and operator||. Unlike the built-in versions, the overloads cannot implement short-circuit evaluation. Also unlike the built-in versions, they do not sequence their left operand before the right one.(until C++17) In the standard library, these operators are only overloaded for std::valarray.
  • The comma operator, operator,. Unlike the built-in version, the overloads do not sequence their left operand before the right one.(until C++17) Because this operator may be overloaded, generic libraries use expressions such as a,void(),b instead of a,b to sequence execution of expressions of user-defined types. The boost library uses in boost.assign, boost.spirit, and other libraries. The database access library SOCI also overloads .
  • The member access through pointer to member operator->*. There are no specific downsides to overloading this operator, but it is rarely used in practice. It was suggested that it could be part of smart pointer interface, and in fact is used in that capacity by actors in boost.phoenix. It is more common in EDSLs such as cpp.react.

[edit]Example

Run this code

Output:

#include <iostream>   class Fraction {int gcd(int a, int b){return b ==0? a : gcd(b, a % b);}int n, d;public: Fraction(int n, int d =1): n(n/gcd(n, d)), d(d/gcd(n, d)){}int num()const{return n;}int den()const{return d;} Fraction& operator*=(const Fraction& rhs){int new_n = n * rhs.n/gcd(n * rhs.n, d * rhs.d); d = d * rhs.d/gcd(n * rhs.n, d * rhs.d); n = new_n;return*this;}};std::ostream& operator<<(std::ostream& out, const Fraction& f){return out << f.num()<<'/'<< f.den();}bool operator==(const Fraction& lhs, const Fraction& rhs){return lhs.num()== rhs.num()&& lhs.den()== rhs.den();}bool operator!=(const Fraction& lhs, const Fraction& rhs){return!(lhs == rhs);} Fraction operator*(Fraction lhs, const Fraction& rhs){return lhs *= rhs;}   int main(){ Fraction f1(3, 8), f2(1, 2), f3(10, 2);std::cout<< f1 <<" * "<< f2 <<" = "<< f1 * f2 <<'\n'<< f2 <<" * "<< f3 <<" = "<< f2 * f3 <<'\n'<<2<<" * "<< f1 <<" = "<<2* f1 <<'\n';}
3/8 * 1/2 = 3/16 1/2 * 5/1 = 5/2 2 * 3/8 = 3/4

[edit]Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1458 C++11 taking address of incomplete type that overloads address-of was undefined behavior the behavior is only unspecified

[edit]See Also

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b
a <=> b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
?:

Special operators

converts one type to another related type
converts within inheritance hierarchies
adds or removes cv qualifiers
converts type to unrelated type
C-style cast converts one type to another by a mix of , , and
creates objects with dynamic storage duration
destructs objects previously created by the new expression and releases obtained memory area
queries the size of a type
queries the size of a parameter pack(since C++11)
queries the type information of a type
checks if an expression can throw an exception (since C++11)
queries alignment requirements of a type (since C++11)

[edit]References

  1. ↑Operator Overloading on StackOverflow C++ FAQ
std::string str ="Hello, "; str.operator+=("world");// same as str += "world"; operator<<(operator<<(std::cout, str) , '\n');// same as std::cout << str << '\n';// (since C++17) except for sequencing
// assume the object holds reusable storage, such as a heap-allocated buffer mArray T& operator=(const T& other)// copy assignment{if(this !=&other){// self-assignment check expectedif(other.size!= size){// storage cannot be reused delete[] mArray;// destroy storage in this size =0; mArray = nullptr;// preserve invariants in case next line throws mArray = new int[other.size];// create storage in this size = other.size;}std::copy(other.mArray, other.mArray+ other.size, mArray);}return*this;}
T& operator=(T&& other)noexcept// move assignment{if(this !=&other){// no-op on self-move-assignment (delete[]/size=0 also ok) delete[] mArray;// delete this storage mArray =std::exchange(other.mArray, nullptr);// leave moved-from in valid state size =std::exchange(other.size, 0);}return*this;}
T& T::operator=(T arg)noexcept// copy/move constructor is called to construct arg{ swap(arg);// resources are exchanged between *this and argreturn*this;}// destructor of arg is called to release the resources formerly held by *this
std::ostream& operator<<(std::ostream& os, const T& obj){// write obj to streamreturn os;}std::istream& operator>>(std::istream& is, T& obj){// read obj from streamif(/* T could not be constructed */) is.setstate(std::ios::failbit);return is;}
struct Sum {int sum; Sum(): sum(0){}void operator()(int n){ sum += n;}}; Sum s =std::for_each(v.begin(), v.end(), Sum());
struct X { X& operator++(){// actual increment takes place herereturn*this;} X operator++(int){ X tmp(*this);// copy operator++();// pre-incrementreturn tmp;// return old value}};
class X {public: X& operator+=(const X& rhs)// compound assignment (does not need to be a member,{// but often is, to modify the private members)/* addition of rhs to *this takes place here */return*this;// return the result by reference}   // friends defined inside class body are inline and are hidden from non-ADL lookupfriend X operator+(X lhs, // passing lhs by value helps optimize chained a+b+cconst X& rhs)// otherwise, both parameters may be const references{ lhs += rhs;// reuse compound assignmentreturn lhs;// return the result by value (uses move constructor)}};
struct Record {std::string name;unsignedint floor;double weight;friendbool operator<(const Record& l, const Record& r){returnstd::tie(l.name, l.floor, l.weight)<std::tie(r.name, r.floor, r.weight);// keep the same order}};
inlinebool operator<(const X& lhs, const X& rhs){/* do actual comparison */}inlinebool operator>(const X& lhs, const X& rhs){return rhs < lhs;}inlinebool operator<=(const X& lhs, const X& rhs){return!(lhs > rhs);}inlinebool operator>=(const X& lhs, const X& rhs){return!(lhs < rhs);}
inlinebool operator==(const X& lhs, const X& rhs){/* do actual comparison */}inlinebool operator!=(const X& lhs, const X& rhs){return!(lhs == rhs);}
inlinebool operator==(const X& lhs, const X& rhs){return cmp(lhs,rhs)==0;}inlinebool operator!=(const X& lhs, const X& rhs){return cmp(lhs,rhs)!=0;}inlinebool operator<(const X& lhs, const X& rhs){return cmp(lhs,rhs)<0;}inlinebool operator>(const X& lhs, const X& rhs){return cmp(lhs,rhs)>0;}inlinebool operator<=(const X& lhs, const X& rhs){return cmp(lhs,rhs)<=0;}inlinebool operator>=(const X& lhs, const X& rhs){return cmp(lhs,rhs)>=0;}
struct T { value_t& operator[](std::size_t idx){return mVector[idx];}const value_t& operator[](std::size_t idx)const{return mVector[idx];}};

The assignment operator (operator=) is used to copy values from one object to another already existing object.

Assignment vs Copy constructor

The purpose of the copy constructor and the assignment operator are almost equivalent -- both copy one object to another. However, the copy constructor initializes new objects, whereas the assignment operator replaces the contents of existing objects.

The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it’s really not all that difficult. Summarizing:

  • If a new object has to be created before the copying can occur, the copy constructor is used (note: this includes passing or returning objects by value).
  • If a new object does not have to be created before the copying can occur, the assignment operator is used.

Overloading the assignment operator

Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we’ll get to. The assignment operator must be overloaded as a member function.

This prints:

5/3

This should all be pretty straightforward by now. Our overloaded operator= returns *this, so that we can chain multiple assignments together:

Issues due to self-assignment

Here’s where things start to get a little more interesting. C++ allows self-assignment:

This will call f1.operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves. In this particular example, the self-assignment causes each member to be assigned to itself, which has no overall impact, other than wasting time. In most cases, a self-assignment doesn’t need to do anything at all!

However, in cases where an assignment operator needs to dynamically assign memory, self-assignment can actually be dangerous:

First, run the program as it is. You’ll see that the program prints “Alex” as it should.

Now run the following program:

You’ll probably get garbage output (or a crash). What happened?

Consider what happens in the overloaded operator= when the implicit object AND the passed in parameter (str) are both variable alex. In this case, m_data is the same as str._m_data. The first thing that happens is that the function checks to see if the implicit object already has a string. If so, it needs to delete it, so we don’t end up with a memory leak. In this case, m_data is allocated, so the function deletes m_data. But str.m_data is pointing to the same address! This means that str.m_data is now a dangling pointer.

Later on, when we’re copying the data from str into our implicit object, we’re accessing dangling pointer str.m_data. That leaves us either copying garbage data or trying to access memory that our application no longer owns (crash).

Detecting and handling self-assignment

Fortunately, we can detect when self-assignment occurs. Here’s a better implementation of our overloaded operator= for the Fraction class:

By checking if our implicit object is the same as the one being passed in as a parameter, we can have our assignment operator just return immediately without doing any other work.

Note that there is no need to check for self-assignment in a copy-constructor. This is because the copy constructor is only called when new objects are being constructed, and there is no way to assign a newly created object to itself in a way that calls to copy constructor.

Default assignment operator

Unlike other operators, the compiler will provide a default public assignment operator for your class if you do not provide one. This assignment operator does memberwise assignment (which is essentially the same as the memberwise initialization that default copy constructors do).

Just like other constructors and operators, you can prevent assignments from being made by making your assignment operator private or using the delete keyword:

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

51

52

53

54

55

56

57

58

#include <cassert>

#include <iostream>

 

classFraction

{

private:

intm_numerator;

intm_denominator;

 

public:

    // Default constructor

    Fraction(intnumerator=0,intdenominator=1):

        m_numerator(numerator),m_denominator(denominator)

    {

        assert(denominator!=0);

    }

 

// Copy constructor

Fraction(constFraction&copy):

m_numerator(copy.m_numerator),m_denominator(copy.m_denominator)

{

// no need to check for a denominator of 0 here since copy must already be a valid Fraction

std::cout<<"Copy constructor called\n";// just to prove it works

}

 

        // Overloaded assignment

        Fraction&operator=(constFraction&fraction);

 

friendstd::ostream&operator<<(std::ostream&out,constFraction&f1);

        

};

 

std::ostream&operator<<(std::ostream&out,constFraction&f1)

{

out<<f1.m_numerator<<"/"<<f1.m_denominator;

returnout;

}

 

// A simplistic implementation of operator= (see better implementation below)

Fraction&Fraction::operator=(constFraction&fraction)

{

    // do the copy

    m_numerator=fraction.m_numerator;

    m_denominator=fraction.m_denominator;

 

    // return the existing object so we can chain this operator

    return*this;

}

 

intmain()

{

    Fraction fiveThirds(5,3);

    Fractionf;

    f=fiveThirds;// calls overloaded assignment

    std::cout<<f;

 

    return0;

}

intmain()

{

    Fraction f1(5,3);

    Fraction f2(7,2);

    Fraction f3(9,5);

 

    f1=f2=f3;// chained assignment

 

    return0;

}

intmain()

{

    Fraction f1(5,3);

    f1=f1;// self assignment

 

    return0;

}

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

51

52

53

54

55

56

57

58

59

60

#include <iostream>

 

classMyString

{

private:

    char*m_data;

    intm_length;

 

public:

    MyString(constchar*data="",intlength=0):

        m_length(length)

    {

        if(!length)

            m_data=nullptr;

        else

            m_data=newchar[length];

 

        for(inti=0;i<length;++i)

            m_data[i]=data[i];

    }

 

    // Overloaded assignment

    MyString&operator=(constMyString&str);

 

    friendstd::ostream&operator<<(std::ostream&out,constMyString&s);

};

 

std::ostream&operator<<(std::ostream&out,constMyString&s)

{

    out<<s.m_data;

    returnout;

}

 

// A simplistic implementation of operator= (do not use)

MyString&MyString::operator=(constMyString&str)

{

    // if data exists in the current string, delete it

    if(m_data)delete[]m_data;

 

    m_length=str.m_length;

 

    // copy the data from str to the implicit object

    m_data=newchar[str.m_length];

 

    for(inti=0;i<str.m_length;++i)

        m_data[i]=str.m_data[i];

 

    // return the existing object so we can chain this operator

    return*this;

}

 

intmain()

{

    MyString alex("Alex",5);// Meet Alex

    MyString employee;

    employee=alex;// Alex is our newest employee

    std::cout<<employee;// Say your name, employee

 

    return0;

}

intmain()

{

    MyString alex("Alex",5);// Meet Alex

    alex=alex;// Alex is himself

    std::cout<<alex;// Say your name, Alex

 

    return0;

}

// A better implementation of operator=

Fraction&Fraction::operator=(constFraction&fraction)

{

    // self-assignment guard

    if(this==&fraction)

        return*this;

 

    // do the copy

    m_numerator=fraction.m_numerator;

    m_denominator=fraction.m_denominator;

 

    // return the existing object so we can chain this operator

    return*this;

}

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

#include <cassert>

#include <iostream>

 

classFraction

{

private:

intm_numerator;

intm_denominator;

 

public:

    // Default constructor

    Fraction(intnumerator=0,intdenominator=1):

        m_numerator(numerator),m_denominator(denominator)

    {

        assert(denominator!=0);

    }

 

// Copy constructor

Fraction(constFraction&copy)=delete;

 

// Overloaded assignment

Fraction&operator=(constFraction&fraction)=delete;// no copies through assignment!

 

friendstd::ostream&operator<<(std::ostream&out,constFraction&f1);

        

};

 

std::ostream&operator<<(std::ostream&out,constFraction&f1)

{

out<<f1.m_numerator<<"/"<<f1.m_denominator;

returnout;

}

 

intmain()

{

    Fraction fiveThirds(5,3);

    Fractionf;

    f=fiveThirds;// compile error, operator= has been deleted

    std::cout<<f;

 

    return0;

}

Leave a Comment

(0 Comments)

Your email address will not be published. Required fields are marked *