C + + noncopyable class


In Muduo, there is onenoncopyableClass. Derived classes that inherit this class cannot be copied, but can only be moved. There are two functions involved in copy: copy constructor and copy copy operator. If you declare these two methods as inaccessible or deleted (= delete), you can achieve the effect of non copying.

Declare function asprivate

By declaring copy constructor and copy copy operator as private, the compiler can be prevented from creating these two functions, and the user can be prevented from calling them at the same time, so as to achieve the effect of blocking copy. The implementation is as follows:

class noncopyable {
    noncopyable(const noncopyable&);
    noncopyable& operator=(const noncopyable&);

Article 06 of the third edition of effective c + +This is the implementation.

usage method:

class myClass : private noncopyable {};

Note: private inheritance must be used here to prevent member function and friend function access.

Using delete keyword in C + + 11

Under c + + 11, delete keyword can be used easily.

class noncopyable {
    noncopyable(const noncopyable&) = delete;
    noncopyable& operator=(const noncopyable&) = delete;

Implementation of boost

boot::noncopyableThe first two methods are combined in the implementation of the

class noncopyable {
    //The default constructors and destructors are protected,
    //Creating a noncopyable instance is not allowed, but subclasses are allowed to create instances
    //(that is, allow derived class construction and destructions).
    noncopyable() = default;
    ~noncopyable() = default;

    //Using the delete keyword prevents the compiler from automatically generating copy constructors and copy assignment operators.
    noncopyable(const noncopyable&) = delete;
    const noncopyable& operator=(const noncopyable&) = delete;

Block copy restrictions

Why do many people disable copy (copy) constructors? There are several restrictions on preventing copy:

  • Before C + + 11, the object must have normal copy semantics before it can be put into the container. The object with copy construction disabled cannot be directly put into the container. Of course, you can use the pointer to avoid this, but you fall into the dilemma of managing the pointer yourself (perhaps using the intelligent pointer can alleviate this problem).
  • Mobile semantics exist in C + + 11. You can move data into containers instead of copying it.
  • Another application of copy constructor isPrototype mode, there is no copy constructor in C + +, which may be difficult to implement.


I wrote an HTTP server by imitating Muduo. Welcome to exchange and guide.

reference resources

  • muduo
  • Why many people disable the copy (copy) constructor
  • C + +: non copyable class
  • Boost noncopyable.hpp header file
  • C + + engineering practice (8): value semantics

Recommended Today

Configure Apache to support PHP in the Apache main configuration file httpd.conf Include custom profile in

In Apache’s main configuration file / conf/ http.conf Add at the bottom Include “D:workspace_phpapache-php.conf” The file path can be any In D: workspace_ Create under PHP file apache- php.conf file Its specific content is [html] view plain copy PHP-Module setup LoadFile “D:/xampp/php/php5ts.dll” LoadModule php5_module “D:/xampp/php/php5apache2_2.dll” <FilesMatch “.php$”> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch “.phps$”> SetHandler application/x-httpd-php-source </FilesMatch> […]