• <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++类型

        时间:2023-08-29
          <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++类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:围绕基本类型的 C++ 类包装器 下一篇:C++ 从 LPCTSTR 转换为 const char *

                  相关文章

                  1. <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>