C++ classes forbid copy constructor and copy assign operator

C++ classes forbid copy constructor and copy assign operator

In C++ classes, the compiler can implicitly create default constructors, copy constructors, copy assignment operators, and destructors for the class. Note that the functions generated by these compilers are all public. In order to prevent these functions from being created, we can declare them as private, which prevents the compiler from secretly creating their corresponding version functions.

class Node  
{  
public:  
    Node(int _data = 0) : data(_data) {}  
    int get() const { return data; }  
    void set(int _data) { data = _data; }  
  
private:  
    Node(const Node &);  
    Node &operator=(const Node &);  
  
    int data;  
};  

In the above class definition, when the program attempts to copy the Node object, the compiler will prevent the operation. In this case, as long as the copy constructor and copy assign operator are declared private, there is another way, we can specifically define a base class that prevents copying action. The base class looks like this:

class Uncopyable  
{  
protected:  
    Uncopyable() {} // 允许derived对象构造和析构  
    ~Uncopyable() {}  
private:  
    Uncopyable(const Uncopyable &); // 阻止copying  
    Uncopyable &operator=(const Uncopyable &);  
};  
  
class Node : private Uncopyable  
{  
public:  
    Node(int _data = 0) : data(_data) {}  
    int get() const { return data; }  
    void set(int _data) { data = _data; }  
  
private:  
    int data;  
};  

In this way, in a program, even in a member function or a friend function, try to copy the Node object, the compiler will try to generate a copy constructor or copy assign operator, the "default version" of these functions will try to call its base The corresponding function of the class, but these calls will be blocked because they are private, that is, the copy operation of the object of this class is blocked.

References:

[1] "Effective C++ 3rd Edition" Clause 6


And by reading open source code, you can use macro definitions to prohibit the copy constructor and assignment constructor of a class

The macro definition DISALLOW_COPY_AND_ASSIGN:

#define DISALLOW_COPY_AND_ASSIGN(classname) \
private:                                   \
    classname(const classname &);             \
    classname &operator=(const classname &);

For disabling the copy constructor and assignment constructor of a class in C++, good C++ code should actively manage these 2 operators.

Similar operations are available in caffe, cartographer and Apollo or other well-known libraries.


Macro definition DISALLOW_IMPLICIT_CONSTRUCTORS:

#define DISALLOW_IMPLICIT_CONSTRUCTORS(classname) \
private:                                         \
    classname();                                    \
    DISALLOW_COPY_AND_ASSIGN(classname);

No-argument constructors for classes are prohibited.


Macro definition DECLARE_SINGLETON:

#define DECLARE_SINGLETON(classname)        \
public:                                    \
    static classname *instance() {            \
    static classname instance;              \
    return &instance;                       \
}                                         \
DISALLOW_IMPLICIT_CONSTRUCTORS(classname) \

Singleton class definition, instance() returns a pointer to the same class object. Copy/assignment operators are prohibited.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324896692&siteId=291194637