1

In C, we can just assign one struct to another:

struct vector {
    int x, y;
};

int main (void) {
    struct vector v1 = {1, 1}, v2;
    v2 = v1;
    return 0;
}

The same goes for C++ objects:

class vector {
public:
    int x, y;
    vector (int a = 0, int b = 0) : x(a) , y(b){};
};

int main (void) {
    vector v1(1, 1), v2;
    v2 = v1;
    return 0;
}

Other than classes that have pointers as member variables, and we don't want to assign memory address but values, or maybe assign object from different class - why do we want to overload the = operator in the first place? What would be an example to a time where this is crucial? (And not the examples above?)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Haim
  • 25,446
  • 3
  • 44
  • 78
  • Any textbook will explain this. – David Heffernan Jun 07 '14 at 13:47
  • The list might not be complete but: 1) Pointers for deep copy as you mentioned 2) Passing ownership of resources 3) Performing book-keeping operations before re-assigning the memory . There are probably lots more I can't see now – Marco A. Jun 07 '14 at 13:49
  • Sometimes you want to deny the use of the assignment operator and other times you want to modify the behavior of the assignment operator from the default of a byte by byte copy to some other behavior. – Richard Chambers Jun 07 '14 at 13:55
  • well, that rule of three again gives the examples of pointers . is there any examples which are not class having pointers/assign different object? – David Haim Jun 07 '14 at 13:56

2 Answers2

3

A big reason is that overloading of initialization and assignment allows you to initialize and assign from values of a different type from that of the class. For instance, you can assign a string literal like "abc" which has type const char * (not even a class type) to a std::string.

Another reason is that even if the type being assigned is the same as the target, you do not necessarily want the default semantics (member for member copy). For instance, when a smart pointer object is assigned, it may want to perform reference count management, and also check for self-assignment and do nothing in that case.

Kaz
  • 55,781
  • 9
  • 100
  • 149
  • Agree. It allows for that kind of flexibility, to define how it should behave when assigning a different type of value that you know as a programmer could work with the class you are defining. – Adrián Pérez Jun 07 '14 at 13:55
  • this is an example pf assigning different objects to one eachother – David Haim Jun 07 '14 at 13:57
3

There are many possible scenarios where defining a custom assignment operator might come in handy. These are the ones I can think of off the top of my head:

  • Deep-copying of objects instead of shallow copies (this is what you mentioned)
  • Passing ownership of resources and/or perform book-keeping operations before doing that
  • Define an object transformation from one type to another (if a cast-operator doesn't suit that)
  • Prevent an assignment (e.g. if you're dealing with temporaries)
  • In C++11 this allows you to also move r-values references around with non-trivial logic
  • Perform additional checks (e.g. self assignment)
Marco A.
  • 43,032
  • 26
  • 132
  • 246