Assignment operator

suggest change

The assignment operator is when you replace the data with an already existing(previously initialized) object with some other object’s data. Lets take this as an example:

// Assignment Operator
#include <iostream>
#include <string>

using std::cout;
using std::endl;

class Foo
{
  public:
    Foo(int data)
    {
        this->data = data;    
    }
    ~Foo(){};
    Foo& operator=(const Foo& rhs)
    {
            data = rhs.data; 
            return *this;
    }

    int data;
};

int main()
{
   Foo foo(2); //Foo(int data) called
   Foo foo2(42);
   foo = foo2; // Assignment Operator Called
   cout << foo.data << endl; //Prints 42
}

You can see here I call the assignment operator when I already initialized the foo object. Then later I assign foo2 to foo . All the changes to appear when you call that equal sign operator is defined in your operator= function. You can see a runnable output here: http://cpp.sh/3qtbm

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Copying vs assignment:
* Syntax
* Assignment operator

Table Of Contents
8 Arrays
11 Loops
39 Streams
47 Copying vs assignment
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype