C ++ 11中的默认函数有什么意义? [英] What's the point in defaulting functions in C++11?

查看:153
本文介绍了C ++ 11中的默认函数有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11增加了告诉编译器创建默认实施任何特殊成员职能。虽然我可以看到删除一个函数的值,其中的值明确默认一个函数?

C++11 adds the ability for telling the compiler to create a default implementation of any of the special member functions. While I can see the value of deleting a function, where's the value of explicitly defaulting a function? Just leave it blank and the compiler will do it anyway.

我可以看到的唯一一点是,默认构造函数只有在没有其他构造函数存在时才被创建:

The only point I can see is that a default constructor is only created when no other constructor exists:

class eg {
public:
    eg(int i);
    eg() = default; 
};

但是这真的比现在更好吗?

But is that really better than how you do it now?

class eg {
public:
    eg(int i);
    eg() {}
};

还是我缺少一个用例?

推荐答案

默认的构造函数将有一个声明,该声明将受正常访问规则的约束。例如。你可以使默认的拷贝构造函数保护。没有这些新的声明,默认生成的成员是public的。

A defaulted constructor will have a declaration, and that declaration will be subject to the normal access rules. E.g. you can make the default copy constructor protected. Without these new declarations, the default generated members are public.

这篇关于C ++ 11中的默认函数有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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