C + + class prohibits copy constructor and copy assign operator

Source: Internet
Author: User
Tags class definition

C + + class prohibits copy constructor and copy assign operator

In a C + + class, the compiler can secretly create a default constructor, a copy constructor, a copy assignment operator, and a destructor for class. Note that the functions generated by these compilers are public, and in order to prevent these functions from being created, we can declare them private, which prevents the compiler from secretly creating its corresponding version function.

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 a program attempts to copy a node object, the compiler blocks the operation. That way, as long as you declare the copy constructor and the copy assign operator as private, there is another way that we can specifically define a base class that blocks copying actions. This base class is shown below:

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 case, in the program, even in the member function or friend function, trying 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 class corresponding function, However, these calls are blocked because they are private, which prevents the copy operation of the class object.

Resources:

"1" "Effective C + + 3rd edition" clause 6

And by reading open source code, you can use a macro definition to disallow class copy constructors and assignment constructors

Macro definition Disallow_copy_and_assign:

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

Used to disallow copy constructors and assignment constructors for class in C + +, good C + + code should proactively 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);

Non-parametric constructors for class are forbidden.

Macro definition Declare_singleton:

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

A singleton class definition, instance () returns a pointer to the same class object. Disables copy/assignment operators.

C + + class prohibits copy constructor and copy assign operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.