条款6:若不想使用编译器自动生成函数,就该明确拒绝

  前面我们已经知道,编译器会默认为我们合成六个成员函数分别是:构造函数、拷贝构造函数、析构函数、赋值运算符重载、取地址运算符重载、const取地址运算符重载。
  那么如果我们如果不想要编译器为我们自动生成某些函数(尤其是复制和赋值构造函数)应该怎么办呢?
  有一个简单的办法,就是将这个两个函数声明放在private中,而不去定义它们。这样,类的用户无法直接对这个类的对象进行赋值和复制。而且,因为你只有声明,而没有定义,所以即使类的成员函数或者友元触发了相关的操作,也会因为没有定义,而发生一个连接错误。在C++中的iostream库中,就是这样处理的。scopedptr也是这样处理的:
  scopedptr代码如下:
  

#include<iostream>
using namespace std;
#include<cassert>
template<class T>
class Scopedptr
{
public:
    Scopedptr(T* ptr = nullptr) :_ptr(ptr)
    {}
    T *operator->()
    {
        return _ptr;
    }
    T &operator*()
    {
        return *_ptr;
    }
    void reset(T *ptr = nullptr)
    {
        delete _ptr;
        _ptr = ptr;
        ptr = nullptr;
    }
    void swap(Scopedptr<T> &sp)
    {
        swap(_ptr, sp._ptr);
    }
    ~Scopedptr()
    {
        if (_ptr)
        {
            delete _ptr;
        }
    }
private:
    Scopedptr(const Scopedptr<T> &sp);//加const是个好习惯
    Scopedptr<T>& operator=(const Scopedptr<T> &sp);
    T *_ptr;
};
void test()
{
    while (1)
    {
        int *ptr = new int[100*1024*1024];
        Scopedptr<int> sp1(ptr);
    }
}
int main()
{
    test();
    system("pause");
}

  还有一种简单的办法,能达到同样的效果,但是却能将错误提前到编译时候解决。它也用到了前一条款用到的性质:如果基类的赋值、复制构造函数是私有的,那么派生类将不会默认构造自己的复制、赋值构造函数。
  

class Uncopyable
{
public:
    Uncopyable(){}
    ~Uncopyable(){}
private:
    Uncopyable(const Uncopyable&);
    Uncopyable& operator=(const Uncopyable&);
};

class Test :public Uncopyable
{
public:
    Test(int val) :value(val){}
    ~Test(){}
private:
    int value;

};
int main()
{
    Test t1(3);
    Test t2(t1);
}

相似的如果想只在堆上生成对象或者只在栈上生成对象也是类似的原理
详细见我之前的博客
http://blog.csdn.net/gjggj/article/details/68633297

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值