zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 6: Explicitly disallow the use of compiler-generated functions you do not want.

    To disallow functionality automatically provided by compilers:

    1. declare the corresponding member functions private and give no implementations.

    class HomeForSale 
    {
    public:
        //...
    private:
        //...
        HomeForSale(const HomeForSale&);        // declarations only
        HomeForSale& operator=(const HomeForSale&);
    };

    2. Using a base class like Uncopyable is one way to do this

    class Uncopyable 
    {
    protected:                // allow construction
        Uncopyable() {}        // and destruction of
        ~Uncopyable() {}    // derived objects...
    private:
        Uncopyable(const Uncopyable&);        // ...but prevent copying
        Uncopyable& operator=(const Uncopyable&);
    };
    
    /*To keep HomeForSale objects from being copied, all we have to do now is inherit from Uncopyable :*/
    class HomeForSale : private Uncopyable 
    { 
        // class no longer declares copy ctor or copy assign. operator
    };
  • 相关阅读:
    VMware Workstation安装CentOs7固定ip地址
    使用阿里云oss
    使用Yapi展示你的api接口
    .net core使用MQTT
    CentOS 7服务器安装brook和bbr加速
    博客主题
    自定义控件
    winform数据绑定
    is as 运算符
    反射
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15191320.html
Copyright © 2011-2022 走看看