zoukankan      html  css  js  c++  java
  • default & delete

    一、使用“=default”

    1. 显式生成拷贝控制成员的合成版本

    class A {
    public:
    	A() = default;
    	A(const A &) = default;
    	A& operator=(const A &) = default;
    	~A() = default;
    }; 

    2. 只能对具有合成版本的成员函数使用“=default”

    • 默认构造函数
    • 拷贝控制成员(拷贝构造函数、拷贝赋值运算符、析构函数、移动构造函数、移动赋值运算符)

    3. 使用“=default”生成的合成函数分为内联的和非内联的

    class A {
    public:
    	A() = default;						// 在类内用=default-->内联的 
    	A(const A &) = default;				// 在类内用=default-->内联的 
    	A& operator=(const A &);
    };
    
    A& A::operator=(const A &) = default;	// 在类外用=default --> 非内联的 
    

      

    二、使用“=delete”

    1. 将相关的函数定义为删除的函数

    • 删除的函数:我们虽然声明了它,但不能以任何方式使用它。

    2. =delete必须出现在函数第一次声明的时候

    3. 可以对任何函数指定=delete

    • 删除的函数的主要用途还是禁止拷贝控制成员

    4. 析构函数不能使用“=delete”

    5. 示例

    class A {
    public:
    	A() = default;						// 使用合成的默认构造函数 
    	A(const A &) = delete;				// 阻止拷贝 
    	A& operator=(const A &) = delete;	// 阻止赋值 
    	~A() = default;						// 使用合成的析构函数 
    };
    
  • 相关阅读:
    FHQ Treap(无旋 Treap)详解
    [CSP-S 2021] 廊桥分配 题解
    Splay Tree(伸展树)详解
    爬虫工程师也应该会的 NodeJS 知识(一)
    网站加密和混淆技术总结
    ip地址
    索引
    go try。。。catch
    python常见问题
    python实现发布订阅
  • 原文地址:https://www.cnblogs.com/xzxl/p/8975759.html
Copyright © 2011-2022 走看看