Home c++ Travel Designer

Travel Designer

Author

Date

Category

Why do you need a displacement designer if there is a movement operator (operator = (t & amp ;?) )?


Answer 1, Authority 100%

use area

Designer and movement operator are used by the compiler in different situations:

  • design designer is used in places where the ad is the definition (initialization) RVALUE-link on the instance of the same class, or by direct initialization In the class / structure constructor (if the definition occurs with LVALUE-links , it will call the copy constructor );
  • Travel operator is used in places where the class instance has already been previously defined and it uses operator = , which as an argument adjusts RVALUE-link On an instance of the same class (if the operator takes LVALUE-link , it will call Assignment Operator ).

about RVALUE links You can read here , here and here .


Control Example (to clarify the differences in the work of these structures)

# include & lt; vector & gt;
Class Buffer.
{
Public:
  Buffer (Const Std :: String & Amp; Buff)
  : pbuff (nullptr)
  , buffsize (buff.length ())
  {
    pbuff = new char [buffsize];
    Memcpy (PBuff, buff.c_str (), buffsize);
  }
  ~ Buffer () {Destroy (); }
  Buffer (Const Buffer & Amp; Other)
  : pbuff (nullptr)
  , BuffSize (Other.BuffSize)
  {
    pbuff = new char [buffsize];
    Memcpy (PBuff, Other.pbuff, BuffSize);
  }
  Buffer & amp; Operator = (Const Buffer & Amp; Other)
  {
    Destroy ();
    BuffSize = Other.Buffsize;
    pbuff = new char [buffsize];
    Memcpy (PBuff, Other.pbuff, BuffSize);
    RETURN * THIS;
  }
  Buffer (Buffer & Amp; & amp; TMP)
  : pBuff (TMP.PBUFF)
  , BuffSize (TMP.BuffSize)
  {
    tmp.pbuff = nullptr;
  }
  Buffer & amp; Operator = (Buffer & Amp; & amp; TMP)
  {
    Destroy ();
    buffsize = tmp.buffsize;
    PBuff = TMP.PBuff;
    tmp.pbuff = nullptr;
    RETURN * THIS;
  }
Private:
  Void Destroy ()
  {
    if (pbuff)
      delete [] pbuff;
  }
  char * pbuff;
  Size_t Buffsize;
};
Buffer CreateBuffer (Const Std :: String & Amp; Buff)
{
  Buffer RetBuff (buff);
  RETURN RETBUFF;
}
INT MAIN ()
{
  Buffer Buffer1 = CreateBuffer ("123"); // Troubleshoot Constructor
  Buffer Buffer2 = Buffer1; // Convects the copy constructor
  Buffer2 = CreateBuffer ("123"); // drives the displacement designer, then the movement operator
  buffer2 = buffer1; // The assignment operator works
}

Supplement

in C++ 11 Each class, in addition to the default designer, has the following 5 default operations:

  1. Copy Constructor;
  2. Copy Assignment;
  3. Move Constructor;
  4. Move Assignment;
  5. Destructor (Destructor).

When determining one of these 5 operations, it is recommended to explicitly specify (or determine or declare using Default or Delete ) all others, because All these 5 operations are closely related. This will contribute to the best understanding of the class semantics when reading the code.
If one of the above-mentioned 5 operations is clearly defined (including using Default or Delete ), then:

  • The missing copy operations will be determined automatically with
    default behavior;
  • The missing displacement operations will not be defined.

This should be considered when writing classes.


Answer 2, Authority 100%

The question actually sounds like “why designers are needed”. The question in principle does not relate to “displacements”, but actually comes down to the fundamental difference between the designers (copying, movement, etc.) and other class member functions (assignment operators, etc.)

The designer in general works on the “cheese” (non-constructed, non-conventional) memory block. At the time of starting the design of the object, as such, it still does not exist and it has no predictable state. Accordingly, the design of the designer is reduced to the creation / initialization of the new object in the “raw” block provided by the block. The copy constructor, for example, copies this state of a certain sample object, the displacement designer – moves, conversion constructor – converts, etc. The displacement designer is in no way allocated from this row.

The assignment operator always deals with an already initialized / designed object in some predictable “valid” state. The work of the assignment operator is reduced by the original state of the object (resource release, for example), followed by copying (or moving, or conversion, etc.) of a new state of a certain source object.

That’s all. Those. Assignment operators generally make more work than designers. Assignment operators destroy the old state of the object and create a new one. And there are nothing to destroy the designers – they only create a new state.

As part of this logic, both the displacement designer and the moving assignment operator cannot be allocated from the total row. Therefore, it is not clear where the question could arise like “why do you need a movement designer if there is a movement operator?”.


Answer 3

In principle, the standard implementation of the assignment operator could do the following: for the object on the left side of the assignment operator, call the destructor first, and then the copy constructor, having transferred it to the object to the object in the right-hand side. Probably it would be better than just copying all fields from the right object in the left, but still it would not be quite right. The fact is that when copying an object you need to highlight a new resource, but it can potentially end in failure. If this happens, then after an unsuccessful attempt to assign an object in the left side will remain in incorrect condition: the old resource is already released (caused by the destructor), and the new capture did not work. Therefore, when overloading the assignment operator, you must always capture a new resource first, and only if this operation has been successful, freeing the old, replacing it with a new one (for this reason, the assignment operator in the adopted response is not entirely correct). In the case of moving, in principle, there is no such problem: only the reference to the resource is copied, and the old resource that the object owned on the left side of the movement operator can be released painlessly, both before copying the reference and after. Most likely, similar to copying the separation on the constructor and the operator in case of displacement is done for the case when the failure ended the release of the resource on the left side of the movement operator. In this situation, it is necessary that the object in the right-hand side continue to own the resource. In this way, the copying and movement atomicity is maintained: either we managed to completely create a copy of the object or merge the resource from one object to another, or copying / moving passed unsuccessfully, and none of the objects were changed.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions