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

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

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

        如何在 C++ 中定义密封类?

        时间:2023-08-02

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

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

                  <bdo id='IUMbw'></bdo><ul id='IUMbw'></ul>
                  <tfoot id='IUMbw'></tfoot>
                    <tbody id='IUMbw'></tbody>
                • 本文介绍了如何在 C++ 中定义密封类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何阻止该类被其他类继承.

                  How to stop the class to be inherited by other class.

                  推荐答案

                  C++11 解决方案

                  在 C++11 中,你可以在定义中使用 final 关键字来封装一个类:

                  C++11 solution

                  In C++11, you can seal a class by using final keyword in the definition as:

                  class A final  //note final keyword is used after the class name
                  {
                     //...
                  };
                  
                  class B : public A  //error - because class A is marked final (sealed).
                  {                   //        so A cannot be derived from.
                     //...
                  };
                  

                  要了解 final 的其他用途,请在此处查看我的回答:

                  To know the other uses of final, see my answer here:

                  • final"的目的是什么?C++11 中函数的关键字?

                  Bjarne Stroustrup 的代码:我可以阻止人们从我的班级?

                  class Usable;
                  class Usable_lock {
                      friend class Usable;
                  private:
                      Usable_lock() {}
                      Usable_lock(const Usable_lock&) {}
                  };
                  
                  class Usable : public virtual Usable_lock {
                  public:
                      Usable();
                      Usable(char*);
                  };
                  Usable a;
                  
                  class DD : public Usable { };
                  
                  DD dd;  // error: DD::DD() cannot access
                          // Usable_lock::Usable_lock(): private  member
                  


                  Generic_lock

                  所以我们可以利用模板使Usable_lock足够通用以密封任何类:


                  Generic_lock

                  So we can make use of template to make the Usable_lock generic enough to seal any class:

                  template<class T>
                  class  Generic_lock 
                  {
                      friend T;
                      Generic_lock() {}                     //private
                      Generic_lock(const Generic_lock&) {}  //private
                  };
                  
                  class Usable : public virtual Generic_lock<Usable>
                  {
                  public:
                      Usable() {}
                  };
                  
                  Usable a; //Okay
                  class DD : public Usable { };
                  
                  DD dd; //Not okay!
                  

                  这篇关于如何在 C++ 中定义密封类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++中的结构继承 下一篇:C++:深度复制基类指针

                  相关文章

                  最新文章

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

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

                    1. <legend id='syr2U'><style id='syr2U'><dir id='syr2U'><q id='syr2U'></q></dir></style></legend>