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

      <tfoot id='lNayS'></tfoot>

    1. <small id='lNayS'></small><noframes id='lNayS'>

    2. 防止 C++ 中的类继承

      时间:2023-08-02
      <legend id='hiQoT'><style id='hiQoT'><dir id='hiQoT'><q id='hiQoT'></q></dir></style></legend>
        <tbody id='hiQoT'></tbody>

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

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

                <tfoot id='hiQoT'></tfoot>
              • 本文介绍了防止 C++ 中的类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                最近我的一位朋友问我如何防止 C++ 中的类继承.他希望编译失败.

                Recently one of my friend asked me how to prevent class inheritance in C++. He wanted the compilation to fail.

                我想了想,找到了 3 个答案.不确定哪个是最好的.

                I was thinking about it and found 3 answers. Not sure which is the best one.

                1) 私有构造函数

                class CBase
                {
                
                public:
                
                 static CBase* CreateInstance() 
                 { 
                  CBase* b1 = new CBase();
                  return b1;
                 }
                
                private:
                
                 CBase() { }
                 CBase(CBase3) { }
                 CBase& operator=(CBase&) { }
                
                
                };
                

                2) 使用 CSealed 基类,私有构造函数 &虚拟继承

                2) Using CSealed base class, private ctor & virtual inheritance

                class CSealed
                {
                
                private:
                
                 CSealed() {
                 }
                
                 friend class CBase;
                };
                
                
                class CBase : virtual CSealed
                {
                
                public:
                
                 CBase() {
                 }
                
                };
                

                3) 使用 CSealed 基类,受保护的构造函数 &虚拟继承

                3) Using a CSealed base class, protected ctor & virtual inheritance

                class CSealed
                {
                
                protected:
                
                 CSealed() {
                 }
                
                };
                
                class CBase : virtual CSealed
                {
                
                public:
                
                 CBase() {
                 }
                
                };
                

                以上所有方法确保CBase 类不能被进一步继承.我的问题是:

                All the above methods make sure that CBase class cannot be inherited further. My Question is:

                1. 哪种方法最好?还有其他方法吗?

                1. Which is the best method ? Any other methods available ?

                方法 2 &除非 CSealed 类被虚拟继承,否则 3 将无法工作.这是为什么 ?和vdisp ptr有关系吗??

                Method 2 & 3 will not work unless the CSealed class is inherited virutally. Why is that ? Does it have anything to do with vdisp ptr ??

                附注:

                以上程序是在 MS C++ 编译器 (Visual Studio) 中编译的.参考:http://www.codeguru.com/forum/存档/index.php/t-321146.html

                The above program was compiled in MS C++ compiler (Visual Studio). reference : http://www.codeguru.com/forum/archive/index.php/t-321146.html

                推荐答案

                从 C++11 开始,您可以将 final 关键字添加到您的类中,例如

                As of C++11, you can add the final keyword to your class, eg

                class CBase final
                {
                ...
                

                我可以看到想要这样做的主要原因(以及我来寻找这个问题的原因)是将类标记为不可子类化,以便您可以安全地使用非虚拟析构函数并完全避免使用 vtable.

                The main reason I can see for wanting to do this (and the reason I came looking for this question) is to mark a class as non subclassable so you can safely use a non-virtual destructor and avoid a vtable altogether.

                这篇关于防止 C++ 中的类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么要在 C++ 中为抽象类声明虚拟析构函数? 下一篇:在 C++ 中覆盖 Base 的重载函数

                相关文章

                最新文章

              • <small id='02KgZ'></small><noframes id='02KgZ'>

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

                  • <bdo id='02KgZ'></bdo><ul id='02KgZ'></ul>

                    <tfoot id='02KgZ'></tfoot>