<bdo id='9pwoZ'></bdo><ul id='9pwoZ'></ul>

    <small id='9pwoZ'></small><noframes id='9pwoZ'>

      <i id='9pwoZ'><tr id='9pwoZ'><dt id='9pwoZ'><q id='9pwoZ'><span id='9pwoZ'><b id='9pwoZ'><form id='9pwoZ'><ins id='9pwoZ'></ins><ul id='9pwoZ'></ul><sub id='9pwoZ'></sub></form><legend id='9pwoZ'></legend><bdo id='9pwoZ'><pre id='9pwoZ'><center id='9pwoZ'></center></pre></bdo></b><th id='9pwoZ'></th></span></q></dt></tr></i><div id='9pwoZ'><tfoot id='9pwoZ'></tfoot><dl id='9pwoZ'><fieldset id='9pwoZ'></fieldset></dl></div>
      <legend id='9pwoZ'><style id='9pwoZ'><dir id='9pwoZ'><q id='9pwoZ'></q></dir></style></legend>
    1. <tfoot id='9pwoZ'></tfoot>
      1. 在 C++ 中禁止复制构造函数的最可靠方法是什么?

        What#39;s the most reliable way to prohibit a copy constructor in C++?(在 C++ 中禁止复制构造函数的最可靠方法是什么?)

        <legend id='EkVVB'><style id='EkVVB'><dir id='EkVVB'><q id='EkVVB'></q></dir></style></legend>

        <small id='EkVVB'></small><noframes id='EkVVB'>

          <bdo id='EkVVB'></bdo><ul id='EkVVB'></ul>
          <tfoot id='EkVVB'></tfoot>
            1. <i id='EkVVB'><tr id='EkVVB'><dt id='EkVVB'><q id='EkVVB'><span id='EkVVB'><b id='EkVVB'><form id='EkVVB'><ins id='EkVVB'></ins><ul id='EkVVB'></ul><sub id='EkVVB'></sub></form><legend id='EkVVB'></legend><bdo id='EkVVB'><pre id='EkVVB'><center id='EkVVB'></center></pre></bdo></b><th id='EkVVB'></th></span></q></dt></tr></i><div id='EkVVB'><tfoot id='EkVVB'></tfoot><dl id='EkVVB'><fieldset id='EkVVB'></fieldset></dl></div>

                  <tbody id='EkVVB'></tbody>
                  本文介绍了在 C++ 中禁止复制构造函数的最可靠方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有时需要在 C++ 类中禁止复制构造函数,以便类变为不可复制".当然,operator= 应该同时禁止.

                  Sometimes it's necessary to prohibit a copy constructor in a C++ class so that class becomes "non-copyable". Of course, operator= should be prohibited at the same time.

                  到目前为止,我已经看到了两种方法来做到这一点.方法一是将方法声明为私有,不实现:

                  So far I've seen two ways to do that. Way 1 is to declare the method private and give it no implementation:

                  class Class {
                  //useful stuff, then
                  private:
                      Class( const Class& ); //not implemented anywhere
                      void operator=( const Class& ); //not implemented anywhere
                  };
                  

                  方法 2 是将方法声明为私有并赋予它空"实现:

                  Way 2 is to declare the method private and give it "empty" implementation:

                  class Class {
                  //useful stuff, then
                  private:
                      Class( const Class& ) {}
                      void operator=( const Class& ) {}
                  };
                  

                  IMO 第一个更好 - 即使有一些意外的原因导致从同一个类成员函数调用复制构造函数,稍后也会出现链接器错误.在第二种情况下,直到运行时才会注意到这种情况.

                  IMO the first one is better - even if there's some unexpected reason that leads to the copy constructor being called from the same class member function there'll be a linker error later on. In the second case this scenario will be left unnoticed until the runtime.

                  第一种方法有什么严重的缺点吗?有什么更好的方法,为什么?

                  Are there any serious drawbacks in the first method? What's a better way if any and why?

                  推荐答案

                  第一种方法是 Boost 如何解决它 (源代码),据我所知,没有任何缺点.事实上,链接器错误是该方法的一大优势.您希望错误发生在链接时,而不是发生在您的客户端正在执行您的代码并且它突然崩溃时.

                  The first method is how Boost solves it (source code), as far as I know, there's no drawbacks. In fact, the linker errors are the big advantage of that method. You want the errors to be at link time, not when your client is executing your code and it suddenly crashes.

                  如果您正在使用 Boost,您可以节省一些打字的时间.这与您的第一个示例相同:

                  In case you are using Boost, you can save yourself some typing. This does the same as your first example:

                  #include <boost/utility.hpp>
                  
                  class Class : boost::noncopyable {
                  // Stuff here
                  }
                  

                  这篇关于在 C++ 中禁止复制构造函数的最可靠方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                    <tbody id='sje3t'></tbody>
                      • <tfoot id='sje3t'></tfoot>

                          <i id='sje3t'><tr id='sje3t'><dt id='sje3t'><q id='sje3t'><span id='sje3t'><b id='sje3t'><form id='sje3t'><ins id='sje3t'></ins><ul id='sje3t'></ul><sub id='sje3t'></sub></form><legend id='sje3t'></legend><bdo id='sje3t'><pre id='sje3t'><center id='sje3t'></center></pre></bdo></b><th id='sje3t'></th></span></q></dt></tr></i><div id='sje3t'><tfoot id='sje3t'></tfoot><dl id='sje3t'><fieldset id='sje3t'></fieldset></dl></div>

                          <small id='sje3t'></small><noframes id='sje3t'>

                          <legend id='sje3t'><style id='sje3t'><dir id='sje3t'><q id='sje3t'></q></dir></style></legend>

                            <bdo id='sje3t'></bdo><ul id='sje3t'></ul>