仅对C ++中的空私有构造函数使用声明是否正确? [英] Is it correct to use declaration only for empty private constructors in C++?

查看:56
本文介绍了仅对C ++中的空私有构造函数使用声明是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,这是否正确:

class C 
{
   private: 
     C();
     C(const & C other);
}

或者您应该提供定义:

class C 
{
   private: 
     C() {};
     C(const & C other) {};
}


感谢您提供当前的答案。让我们扩展这个问题-编译器是否可以在以下示例之一中生成更好的代码?我可以想象为ctor提供主体会迫使编译器在编译单元中包含一些(空)代码吗?

? Thanks for the current answers. Let's extend this question - does compiler generate better code in one of this examples? I can imagine that providing body for ctor forces compiler to include some (empty) code in compilation unit? Is this also true for auto-generated code?

推荐答案

如果您不希望对象是可复制的,那么就没有了。需要提供执行。只需将复制ctor声明为私有即可,无需任何实现。对于其他ctor来说也是如此,如果您不希望任何人使用它们,只需将其声明为私有即可,无需任何实现。

If you do not wish your object to be copyable, then there is no need to provide the implementation. Just declare the copy ctor private without any implementation. The same holds for other ctors, if you do not want any body to use them, just declare them private without any implementation.

这篇关于仅对C ++中的空私有构造函数使用声明是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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