简体   繁体   English

重载operator =和重载构造函数重载有什么区别?

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

What is the difference between overloading the operator = in a class and the copy constructor ? 重载operator =在类和复制构造函数之间有什么区别?

In which context is each one called? 在哪个上下文中被称为?

I mean, if I have the following: 我的意思是,如果我有以下内容:

Person *p1 = new Person("Oscar", "Mederos");
Person *p2 = p1;

Which one is used? 使用哪一个? And then when the other one is used? 然后当使用另一个?

Edit: 编辑:
Just to clarify a little bit: 只是澄清一下:

I already know that if we explicitly call the copy constructor Person p1(p2) , the copy constructor will be used. 我已经知道如果我们显式调用复制构造函数Person p1(p2) ,将使用复制构造函数。 What I wanted to know is when each one is used, but using the = operator instead, as @Martin pointed. 我想知道的是每个使用的时候,但是使用=运算符代替,如@Martin指出的那样。

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: 值得一提的是,赋值运算符可以使用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;
    }
};

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); 实际上,编译器假定Person p1; Person p2(p1); Person p1; Person p2(p1); entails p1 == p2; 需要p1 == p2; .

Lastly, as a final aside and as said elsewhere, note that Person p = p2; 最后,作为最后的一边,如其他地方所说,请注意Person p = p2; literally means Person p(p2) (copy construction), and never Person p; p = p2; 字面意思Person p(p2) (复制结构),而不是 Person p; p = p2; 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). 这是一种语法糖,允许您在不影响效率的情况下编写自然的代码(甚至是正确性,因为您的类甚至可能不是默认构造的)。

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. 重载赋值运算符将现有对象的内容分配给同一类的另一个现有对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM