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;						// 使用合成的析构函数 
    };
    
  • 相关阅读:
    五分钟上手Markdown
    css中居中方法小结
    事务和同步锁
    插入排序
    插入排序
    交换排序
    eclipse 常用快捷键
    交换排序
    二叉搜索树(BST)
    二叉树遍历以及根据前序遍历序列反向生成二叉树
  • 原文地址:https://www.cnblogs.com/xzxl/p/8975759.html
Copyright © 2011-2022 走看看