Why is my overloaded assignment operator is not being invoked on initialization? : <p>I created a test class to demonstrate the use of the overloaded assignment operator, and the pitfalls associated with double freeing memory. The code for this class is below:</p>

<pre><code>class Test {
  public:
    Test() : ptr{new int(0)}, val{0}, id{count++} {}

    Test(int ptr_val, int new_val) : ptr{new int(ptr_val)}, val{new_val}, id{count++} {}

    ~Test()
    {
        delete ptr;
    }

    Test&amp; operator=(const Test&amp; rhs)
    {
        *ptr = *(rhs.ptr);
        val = rhs.val;
    }

    void setPtrVal(int ptr_val)
    {
        *ptr = ptr_val;
    }

    void setVal(int new_val)
    {
        val = new_val;
    }

    void printData() const
    {
        cout &lt;&lt; "id  = " &lt;&lt; id  &lt;&lt; endl;
        cout &lt;&lt; "val = " &lt;&lt; val &lt;&lt; endl;
        cout &lt;&lt; "ptr = " &lt;&lt; ptr &lt;&lt; endl;
        cout &lt;&lt; "*ptr = " &lt;&lt; *ptr &lt;&lt; endl &lt;&lt; endl;
    }
  private:
    int* ptr;
    int val;
    int id;

    static int count;
};

int Test::count = 1;
</code></pre>

<p>And I am having two main functions which are testing this class with the overloaded assignment operator. I am putting the output for each main function directly below it's body.</p>

<p>Main 1:</p>

<pre><code>int main()
{
    Test t1;
    Test t2(2, 2);

    t1.printData();
    t2.printData();

    t2 = t1;  // Overloaded Assignment Operator
    t1.printData();
    t2.printData();

    t2.setVal(3);
    t2.setPtrVal(3);

    t1.printData();
    t2.printData();

    return 0;
}
</code></pre>

<p>Output 1: (works as expected)</p>

<pre><code>id  = 1
val = 0
ptr = 0x204dc20
*ptr = 0

id  = 2
val = 2
ptr = 0x204dc40
*ptr = 2

id  = 1
val = 0
ptr = 0x204dc20
*ptr = 0

id  = 2
val = 0
ptr = 0x204dc40
*ptr = 0

id  = 1
val = 0
ptr = 0x204dc20
*ptr = 0

id  = 2
val = 3
ptr = 0x204dc40
*ptr = 3
</code></pre>

<p>Main 2:</p>

<pre><code>int main()
{
    Test t1(10, 15);

    {
        Test t2 = t1;
        t2.printData();
    }

    t1.printData();

    return 0;
}
</code></pre>

<p>Output 2 (does not work as expected):</p>

<pre><code>id  = 1
val = 15
ptr = 0xd6fc20
*ptr = 10

id  = 1
val = 15
ptr = 0xd6fc20
*ptr = 0

*** Error in `./a.out': double free or corruption (fasttop): 0x0000000000d6fc20 ***
</code></pre>

<p>This is weird. For some reason, the pointer points to the same memory, and the <code>id</code> field is the same. It should be different. It appears that the default assignment operator is invoked. I don't know why this is the case.</p>

<p>Don't try to criticize the program itself. This is just a test for educational purposes. I set this up on purpose so that I would have a double free bug. The overloaded assignment operator is meant to prevent that. And it fails the second test... Why it does that?</p>
