2

In the below given code, I have written cout statements in all the class constructors, destructor and overloaded assignment operator.

#include <iostream>
using namespace std;

class person {
    string name;
    int age ;
    int id ;
    static int num;
public :
    person (string name , int age) : name(name) , age(age) {
        id = num++;
        cout << "creating person : " << id << "(" << name <<")"<< endl;
    }
    person (const person &other) : name(other.name) , age(other.age) {
            id = num++;
            cout << "CREATING PERSON  : " << id << "(" << name <<")" << " from : " << other.id << endl;
    }
    ~person () {
        cout << "killing person : " << id << "(" << name <<")" << endl;
    }
    const person operator= (const person &other) {
        name = other.name ;
        age = other.age;
        //id = num++;
        cout << "copying in : " << id << "(" << name <<")" << " from : " << other.id << endl;
        return *this;
    }
    void print () {
        cout << "name : " << name << ", age : " << age << ", id : " << id << endl;
    }

int person::num = 1;

int main() {
    person per1 ("p1" , 20);
    person per2 ("p2" , 30);
    person per3 ("p3" , 40);
    cout << "see the strange object creation here: " << endl << endl;
    per3 = per2 = per1;
    return 0;
}

The output of the given code comes out to be:

creating person : 1(p1)
creating person : 2(p2)
creating person : 3(p3)
see the strange object creation here:
copying in : 2(p1) from : 1
*CREATING PERSON  : 4(p1) from : 2*
copying in : 3(p1) from : 4
*CREATING PERSON  : 5(p1) from : 3*
killing person : 5(p1)
killing person : 4(p1)
killing person : 3(p1)
killing person : 2(p1)
killing person : 1(p1)

My question is, what caused two objects (4 and 5) to be created with the copy constructor? The objects used in assignment were already existing. Is there a way to overload the assignment operator without creating dummy objects? This method does not seem very optimized.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Vishesh Arya
  • 39
  • 1
  • 10

1 Answers1

8

That's because your operator=() looks like this:

const person operator= (const person &other)

That returns by value. const doesn't really make sense in this context.

What you actually mean to do is return by constant reference instead:

const person& operator= (const person &other)

That one & will make all the difference.

  • 1
    wow! it does make all the difference. i am no longer creating the unwanted objects .thanks a lot for the insight. can you please elaborate exactly where i was creating the copy in my previous code? was it during returning "this" or during assigning "this" to the other object? – Vishesh Arya May 22 '20 at 03:36
  • You're welcome. If you want, you upvote this answer or mark this answer as accepted by clicking the check mark next to it if it has solved your problem. –  May 22 '20 at 03:46
  • @VisheshArya I think you're right on the inability to vote, but if you can't select this as the answer now, you should be able to do it soon. You should mark answers that showed you how to solve the problem with the check mark to show future questioners that the question has been successfully answered. – user4581301 May 22 '20 at 03:53
  • 1
    @VisheshArya Some recommended reading that should answer a few of your questions: [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – user4581301 May 22 '20 at 03:55
  • 3
    @VisheshArya "*can you please elaborate exactly where i was creating the copy in my previous code?*" - when returning `*this` **by value**, a copy is made for the object being `return`'ed. Changing the return value from `person` to `person&` (you should not be returning a `const` from `operator=`) avoids that copy being made. – Remy Lebeau May 22 '20 at 04:18
  • Thank you, @RemyLebeau I somehow missed that part of the comment. –  May 22 '20 at 04:19
  • thanks a lot @RemyLebeau. that clears a lot for me. i wanted to know when exactly the copy is made and your answer is quite on the point. – Vishesh Arya May 22 '20 at 09:24