• <bdo id='gLHwW'></bdo><ul id='gLHwW'></ul>

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

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

        静态成员函数中封闭类的C++类型

        C++ type of enclosing class in static member function(静态成员函数中封闭类的C++类型)
          <tbody id='9Pkwp'></tbody>
        <tfoot id='9Pkwp'></tfoot>

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

              <legend id='9Pkwp'><style id='9Pkwp'><dir id='9Pkwp'><q id='9Pkwp'></q></dir></style></legend>
            1. <small id='9Pkwp'></small><noframes id='9Pkwp'>

                  本文介绍了静态成员函数中封闭类的C++类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我认为这是完全不可能的,但是如果.是否可以在任何版本的 C++ 中以某种方式在静态成员函数中获取封闭类的类型?

                  I assume this is outright impossible, but what if. Is it possible to somehow get type of enclosing class in a static member function, in any version of C++?

                  class Impossible {
                  public:
                      static void Fun()
                      {
                          typedef Impossible EnclosingClass;
                  
                          // now do something with EnclosingClass ...
                      }
                  }
                  

                  有没有办法在不将类名写入函数的情况下获取封闭类的类型(在本例中为Impossible)?

                  Is there a way to get the type of the enclosing class (Impossible in this case) without writing the name of the class in the function?

                  我想这样做的原因是为了避免在函数中重复类名.如果发生这样的事情,很容易导致难以找到的复制粘贴错误:

                  The reason why I'd like to do that is to avoid repeating the class name in the function. It could easily lead to a hard to find copy-paste bug, if something like this happened:

                  class SomeOther { // another class, with the same interface as Impossible
                  public:
                      static void Fun()
                      {
                          typedef Impossible EnclosingClass;
                          // whoops, copy-pasted, forgot to change "Impossible" to "SomeOther"
                  
                          // now do something with EnclosingClass ...
                      }
                  }
                  

                  有什么好的方法可以防止这种事情发生吗?我可以想象触及在封闭类中声明为私有的东西,但这将迫使我编写额外的代码(因为我当前的设计不包含任何固有的私有成员,所有成员都是公开的).

                  Is there a good way to prevent this kind of thing happening? I could imagine touching something that was declared private in the enclosing class, but that would be forcing me to write extra code (as my current design doesn't contain any inherent private members, all is public).

                  推荐答案

                  问题在于 C++ 缺少 self 关键字.

                  The problem is that C++ is lacking a self keyword.

                  我通常写:

                  struct Foo
                  {
                     typedef Foo self;
                  
                     static void bar()
                     {
                        self* ptr = nullptr;
                     }
                  };
                  

                  我意识到你仍然需要确保 typedef 是正确的,但至少这样你可以将它放在类型定义的顶部,你会注意到它.

                  I realise you still have to make sure the typedef is correct, but at least this way you can have it at the top of the type definition where you'll notice it.

                  不过,有了hackery,您可以完全自主.

                  With hackery, though, you can make this entirely autonomous.

                  这篇关于静态成员函数中封闭类的C++类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

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

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

                        <legend id='nSFfm'><style id='nSFfm'><dir id='nSFfm'><q id='nSFfm'></q></dir></style></legend>
                        <tfoot id='nSFfm'></tfoot>
                        • <bdo id='nSFfm'></bdo><ul id='nSFfm'></ul>