Deleted functions in C++11






The deleted functions feature is introduced into the C++11 standard. In this article, I will explain this feature and provide some examples on how to use it.

 

Background

C++ has six kinds of special member functions:

•Default constructors

•Destructors

•Copy constructors

•Copy assignment operators

•Move constructors

•Move assignment operators

These special member functions are used to create, destroy, initialize, convert, and copy class objects.

 

Suppose that you haven't defined a certain special member function for a class; when such a special member function is needed, the compiler will implicitly declare a default special member function for this class. For example:

Example 1

class X{

public:

  X();

};

 

int main(){

  X x1;

  X x2=x1;        // Fine. A default copy constructor is implicitly

                        // defined and called by the compiler.

  X x3;

  x3=x1;           // Fine. A default copy assignment operator is

                       // implicitly defined and called by the compiler.

}

 

In this example, there is no user-defined copy constructor or copy assignment operator. However, when these two special member functions are needed, the compiler will implicitly declare a default copy constructor and a copy assignment operator.

 

Problems

In some cases, having the compiler define special member functions implicitly can benefit programmers:

•Programmers need not to manually define these special member functions. The manual definition requires more coding effort.

•The compiler implicitly defined member functions are more efficient than the user-defined special member functions.

 

However, it might become a problem if you want to forbid copy or assignment operations between class objects. but you cannot stop the compiler from defining the default copy constructor or copy assignment operator implicitly.

 

To address this problem, C++11 introduced the deleted functions feature.

 

Deleted functions

The C++11 standard introduced deleted function declaration as a new form of function declaration.

 

To declare a deleted function, you can append the “=delete;” specifier to the end of a function declaration. The compiler prohibits the usage of a deleted function. For example, you can declare the implicitly defined copy constructor and copy assignment operator of class X as deleted functions to prevent object copy of that class.

Example 2

class X{

 public:

   X();

   X(const X&) = delete;                                      //deleted copy constructor

   X& operator = (const X &) = delete;                //deleted copy assignment operator

};

 

int main(){

  X x1;

  X x2=x1;         //Error. The usage of the copy constructor is disabled.

  X x3;

  x3=x1;            //Error. The usage of the copy assignment operator is disabled.

}

 

In example 2, although only one copy constructor and one copy assignment operator are declared as deleted functions explicitly, the compiler can detect the declarations of the user-defined copy constructor and copy assignment operator. Thus, the compiler will no longer implicitly define any other copy constructors or copy assignment operators that have different parameters. In this case, the copy and assign operations between class objects are totally prohibited.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值