删除的构造函数必须是私有的吗? [英] Must a deleted constructor be private?

查看:149
本文介绍了删除的构造函数必须是私有的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class A
{
public:
    A() = default;
    A(const A&) = delete;
};

class A
{
public:
    A() = default;

private:
    A(const A&) = delete;
};

在任何情况下,这两个定义是否总是相同的?

Are these two definitions always identical to each other in any cases?

推荐答案

它们是不同的只是生产诊断。如果您将其设置为 private ,则会报告额外的和多余的访问冲突:

They are different only wrt the produced diagnostics. If you make it private, an additional and superfluous access violation is reported:

class A
{
public:
    A() = default;
private:
    A(const A&) = delete;
};

int main()
{
    A a;
    A a2=a;
}

会导致GCC 4.8输出以下附加

results in the following additional output from GCC 4.8:

main.cpp: In function 'int main()':
main.cpp:6:5: error: 'A::A(const A&)' is private
     A(const A&) = delete;
     ^
main.cpp:12:10: error: within this context
     A a2=a;
          ^

因此我建议始终使删除的方法 public

hence my recommendation to always make deleted methods public.

这篇关于删除的构造函数必须是私有的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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