删除修饰符与声明函数为私有 [英] delete modifier vs declaring function as private

查看:108
本文介绍了删除修饰符与声明函数为私有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 问题,但它仍然没有使我有很多的意义。

I read this question, but it still doesn't make a lot of sense to me. It still sounds more like a sugarcoating feature.

有什么区别:

class A 
{
// public/private ?
    A (const A&) = delete; 
};

class A 
{
private:
    A (const A&); // MISSING implementation
};

operator = 或其他函数相同。

推荐答案

一个区别是 = delete -time 错误,而在某些情况下,没有定义的声明只会在链接时(错误消息通常不会将您指向问题的根源)处捕获。一个这样的情况是,当您添加一个成员函数试图复制 A 的实例。即使它不是 A 的成员函数,关于copy-ctor为 private 的错误消息也不清楚因为使用 = delete

One difference is that =delete allows for compile-time errors while in some cases the declaration without a definition is only caught at link-time (at which the error message is typically not pointing you to the source of the problem). One such case is when you add a member function that tries to copy an instance of A. Even when it's not a member function of A, the error message about the copy-ctor being private is not as clear as using =delete.

为了避免混淆,我建议您将删除的函数 public ,否则您会收到额外的误导性错误讯息。

To avoid confusion, I'd recommend you make the deleted function public as otherwise you will get additional and misleading error messages.

这篇关于删除修饰符与声明函数为私有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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