什么是“默认”意味着在类的函数声明之后? [英] What does "default" mean after a class' function declaration?

查看:156
本文介绍了什么是“默认”意味着在类的函数声明之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在类中的函数声明旁边看到 default 。它是做什么的?

I've seen default used next to function declarations in a class. What does it do?

class C {
  C(const C&) = default;
  C(C&&) = default;
  C& operator=(const C&) & = default;
  C& operator=(C&&) & = default;
  virtual ~C() { }
};


推荐答案

这是一个新的C ++ 11功能

这意味着您要使用编译器生成的该函数版本,所以你不需要指定一个body。

It means that you want to use the compiler-generated version of that function, so you don't need to specify a body.

你也可以使用 = delete 指定您不要希望编译器自动生成该函数。

You can also use = delete to specify that you don't want the compiler to generate that function automatically.

通过引入move构造函数和移动赋值运算符,当生成自动版本的构造函数,析构函数和赋值运算符时的规则变得相当复杂。使用 = default = delete 使事情变得更容易,因为您不需要记住规则:你想要发生。

With the introduction of move constructors and move assignment operators, the rules for when automatic versions of constructors, destructors and assignment operators are generated has become quite complex. Using = default and = delete makes things easier as you don't need to remember the rules: you just say what you want to happen.

这篇关于什么是“默认”意味着在类的函数声明之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆