C ++ copy of the stop

1. compiler automatically generates a function

When defining a class, if not declare a constructor, destructor, copy constructor and copy assignment operator, which lack them, will declare a compiler (compiled version of) the corresponding function for it;

2. prevent copy

There are two ways to stop the copy:

Method One :

Under the new standard, and copying the copy constructor is defined as the assignment operator delete function (delete function), using the following method:

Struct NoCopy

{

NoCopy();  //...

NoCopy(const NoCopy&)=delete;

NoCopy& operator=(const NoCopy&)=delete;

//...

}

Method Two:

The copy constructor and copy assignment operator declared private and not implemented using the following method:

class NoCopy

{

NoCopy();  //...

private:

NoCopy(const NoCopy&);

NoCopy& operator=(const NoCopy&);

//...

}

When an object NoCopy go to another subject or assignment to perform the copy operation, there will be a lead member of undefined when a link error, because the function is defined as private private;

When a member function or friend function come into the chant, when a link error will occur because there is no realization;

In summary, a method is usually recommended to prevent the completion of the copy operation.

 

 

Guess you like

Origin www.cnblogs.com/jiayouya-susu/p/11962146.html