简体   繁体   English

调用已删除的的构造函数

[英]Call to deleted constructor of

I have a project in C++ and learning design pattern along the way (I'm very new to C++). 我有一个C ++项目,并且一直学习设计模式(我对C ++很陌生)。 I had a situation where I thought that a singleton would be a solution. 我有一种情况,我认为单身人士是一个解决方案。 (Now wait wait wait before you all go: singletons are baaaaaad. Let's all down vote and burn that heretic user!!!!) (现在等一下,等一下,等一下,一切就绪:单例已经成为坏蛋。让我们全力以赴,烧掉那个异端用户!!!)

I ran the example found here: https://stackoverflow.com/a/1008289/2336887 我运行了以下示例: https : //stackoverflow.com/a/1008289/2336887

...but am getting an error when using the C++ 11 version. ...但是在使用C ++ 11版本时出现错误。

My question is not whether Singleton should or not be used. 我的问题不是是否应该使用Singleton。 It has been covered more than enough. 它已被覆盖得足够多。

My question is: Why delete the public constructor and not simply the keep the private one? 我的问题是:为什么要删除公共构造函数,而不仅仅是保留私有构造函数? If it stays there, the error call to deleted constructor occurs. 如果停留在该位置,则会发生对已删除构造函数的错误调用。 I don't understand and it frustrates me not to. 我不明白,这让我很沮丧。 Could somebody shed some light on a C++ newbie? 有人可以对C ++新手有所启发吗?

Here is the code to avoid going to the other post: 这是避免转到另一篇文章的代码:

 class S {
 public:
     static S& getInstance(){
         static S    instance; 
         return instance;
     }
 private:
     S() {}

 public:
     S(S const&)               = delete;
     void operator=(S const&)  = delete;
 };



 int main() {
     S bus =  S::getInstance();
     return 0;
 }

Thanks for your help... and patience. 感谢您的帮助...和耐心等待。

ps: I could have added the question to the original post, but at this point I think it would have brought more noise than anything. ps:我可以将问题添加到原始帖子中,但是在这一点上,我认为这会带来更多的噪音。

Why delete the public constructor and not simply the keep the private one? 为什么要删除公共构造函数,而不仅仅是保留私有构造函数?

Because the public constructor is a copy constructor. 因为public构造函数是复制构造函数。 That's being deleted because it isn't needed and should not be used. 由于不需要并且不应使用该内容,因此将其删除。 The private constructor is a default constructor, which is needed for internal use (at some point, a singleton must be constructed!) 私有构造函数是默认的构造函数,供内部使用(在某个时候,必须构造一个单例!)。

Why delete the public constructor and not simply the keep the private one? 为什么要删除公共构造函数,而不仅仅是保留私有构造函数?

The core idea of singleton is that there is only ever one instance. 单例的核心思想是只有一个实例。 If copying of the object were allowed, then there could be more than one instance. 如果允许复制对象,则可能有多个实例。 That is why the copy constructor of a singleton is deleted: To make the singleton non-copyable. 这就是删除单例的复制构造函数的原因:使单例不可复制。

If it stays there, the error call to deleted constructor occurs. 如果停留在该位置,则会发生对已删除构造函数的错误调用。 I don't understand and it frustrates me not to. 我不明白,这让我很沮丧。

There is an error because you try to copy a non-copyable object. 因为您尝试复制不可复制的对象,所以出现错误。 Don't try to copy singletons. 不要尝试复制单例。 I suspect that you were suppsed to have a reference to the singleton instead: 我怀疑您应该改为引用单身人士:

S& bus = S::getInstance();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM