What is the difference between overloading operator= and overloading the copy constructor?

后端 未结 3 552
我寻月下人不归
我寻月下人不归 2020-12-20 15:29

What is the difference between overloading the operator = in a class and the copy constructor?

In which context is each one called?

I m

相关标签:
3条回答
  • 2020-12-20 15:51

    In your case neither is used as you are copying a pointer.

    Person p1("Oscar", "Mdderos");
    Person extra;
    

    The copy constructor

    Person P2(p1);      // A copy is made using the copy constructor
    Person P3  = p2;    // Another form of copy construction.
                        // P3 is being-initialized and it uses copy construction here
                        // NOT the assignment operator
    

    An assignment:

    extra = P2;         // An already existing object (extra)
                        // is assigned to.
    

    It is worth mentioning that that the assignment operator can be written in terms of the copy constructor using the Copy and Swap idium:

    class Person
    {
        Person(std::string const& f, std::string const& s);
        Person(Person const& copy);
    
        // Note: Do not use reference here.
        //       Thus getting you an implicit copy (using the copy constructor)
        //       and thus you just need to the swap
        Person& operator=(Person copy)
        {
            copy.swap(*this);
            return *this;
        }
    
        void swap(Person& other) throws()
        {
              // Swap members of other and *this;
        }
    };
    
    0 讨论(0)
  • 2020-12-20 15:52

    The copy constructor is a constructor, it creates an object. In particular, the copy constructor creates an object which is semantically identical to another, already existing object, of which it makes a "copy":

    Person newperson(oldperson); // newperson is a "copy" of oldperson
    

    The assignment operator isn't a constructor at all, but an ordinary member funcion that can only be invoked on an existing object. Its purpose is to assign to your object the semantics of another object, so that after the assignment the two are semantically identical. You are not usually "overloading" the assignment operator, you are just defining it.

    Person p;          // construct new person
    /* dum-dee-doo */
    p = otherperson;   // assign to p the meaning of otherperson, overwriting everything it was before
                       // invokes p.operator=(otherperson)
    

    Note that if it makes sense to compare to objects (with ==), then both copy construction and assignment should behave so that we have equality afterwards:

    Person p1(p2);
    assert(p1 == p2);
    
    p1 = p3;
    assert(p1 == p3);
    

    You are not forced to guarantee this, but users of your class will usually assume this behaviour. In fact, the compiler assumes that Person p1; Person p2(p1); entails p1 == p2;.

    Lastly, as a final aside and as said elsewhere, note that Person p = p2; literally means Person p(p2) (copy construction), and never Person p; p = p2;. That's syntactic sugar to allow you to write naturally-looking code without compromising efficiency (or even correctness, as your class may not even be default-constructible).

    0 讨论(0)
  • 2020-12-20 16:10

    A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

    0 讨论(0)
提交回复
热议问题