久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      1. <legend id='sztQf'><style id='sztQf'><dir id='sztQf'><q id='sztQf'></q></dir></style></legend>
          <bdo id='sztQf'></bdo><ul id='sztQf'></ul>
        <tfoot id='sztQf'></tfoot>
      2. <small id='sztQf'></small><noframes id='sztQf'>

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

        使用模板技巧訪問私人成員

        access private member using template trick(使用模板技巧訪問私人成員)
          <legend id='p3VHe'><style id='p3VHe'><dir id='p3VHe'><q id='p3VHe'></q></dir></style></legend>
              <tbody id='p3VHe'></tbody>

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

                • <bdo id='p3VHe'></bdo><ul id='p3VHe'></ul>
                  <i id='p3VHe'><tr id='p3VHe'><dt id='p3VHe'><q id='p3VHe'><span id='p3VHe'><b id='p3VHe'><form id='p3VHe'><ins id='p3VHe'></ins><ul id='p3VHe'></ul><sub id='p3VHe'></sub></form><legend id='p3VHe'></legend><bdo id='p3VHe'><pre id='p3VHe'><center id='p3VHe'></center></pre></bdo></b><th id='p3VHe'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='p3VHe'><tfoot id='p3VHe'></tfoot><dl id='p3VHe'><fieldset id='p3VHe'></fieldset></dl></div>
                  <tfoot id='p3VHe'></tfoot>
                  本文介紹了使用模板技巧訪問私人成員的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  來自博客文章 訪問私人成員:更安全的骯臟 by Johannes Schaub - litb:

                  template<typename Tag, typename Tag::type M>
                  struct Rob { 
                    friend typename Tag::type get(Tag) {
                      return M;
                    }
                  };
                  
                  // use
                  struct A {
                    A(int a):a(a) { }
                  private:
                    int a;
                  };
                  
                  // tag used to access A::a
                  struct A_f { 
                    typedef int A::*type;
                    friend type get(A_f);
                  };
                  
                  template struct Rob<A_f, &A::a>;
                  
                  int main() {
                    A a(42);
                    std::cout << "proof: " << a.*get(A_f()) << std::endl;
                  }
                  

                  如何從 a 對象調用 get 函數,因為它沒有在 class A 中定義?

                  how get function can be call from a object since its not defined inside class A ?

                  我不明白為什么 get 必須有 Tag 作為參數而不是 a.*get()=> 好的,這是由于 ADL 機制

                  I don't understand why get must have Tag as parameter instead of a.*get<A_f>() => ok it's due to ADL mechanism

                  推薦答案

                  你不是從 a 調用 get!實際上返回的是一個指向 A 內部成員的類指針,它的類型是 int A::* 所以你需要一個 A 的實例> 訪問該值.

                  You are not calling get from a! Actually what get return is a class pointer to a member inside A and type of it is int A::* so you need an instance of A to access that value.

                  例如讓我玩一下你的代碼:

                  For example let me play a little with your code:

                  struct A {
                      A(int a):a(a) { }
                      int b;
                  private:
                      int a;
                  };
                  void test() {
                      auto p = &A::b;
                      std::cout << a.*p << std::endl;
                  }
                  

                  我是否從 a 內部調用了 p ?a 沒有 p,這正是你的代碼中發生的事情,get 函數返回 &A::a 然后你使用 a 來讀取它的值!就是這樣,沒有錯,我認為它會在所有編譯器中編譯.

                  Did I call p from inside a? a does not have p, this is exactly what happened in your code, get function return &A::a and you use a to read its value! that's all, nothing is wrong and I think it will be compiled in all compilers.

                  這里的另一個問題是:為什么 C++ 允許使用 A 的私有成員聲明模板.C++ 標準說:

                  One other question here is: Why C++ allow declaring template using private member of A. C++ standard say:

                  14.7.2p8 通常的訪問檢查規則不適用于用于指定顯式實例化的名稱.[注:特別是模板函數聲明器中使用的參數和名稱(包括參數類型、返回類型和異常規范)可能是通常無法訪問的私有類型或對象模板可以是成員模板或成員函數通常無法訪問.]

                  14.7.2p8 The usual access checking rules do not apply to names used to specify explicit instantiations. [Note: In particular, the template arguments and names used in the function declarator (including parameter types, return types and exception specifications) may be private types or objects which would normally not be accessible and the template may be a member template or member function which would not normally be accessible.]

                  但是,如果您嘗試實例化或什至 typedef 指定的模板,則會出現錯誤.讓我們稍微修改一下您的示例:

                  But if you try to instantiate or even typedef specified template then you get an error. Let's modify your example slightly:

                  struct A {
                  private:
                      int a;
                      friend void f();
                  };
                  
                  // Explicit instantiation - OK, no access checks
                  template struct Rob<A_f, &A::a>;
                  
                  // Try to use the type in some way - get an error.
                  struct Rob<A_f, &A::a> r;            // error
                  typedef struct Rob<A_f, &A::a> R;    // error
                  void g(struct Rob<A_f, &A::a>);      // error
                  
                  // However, it's Ok inside a friend function.
                  void f() {
                      Rob<A_f, &A::a> r;               // OK
                      typedef Rob<A_f, &A::a> R;       // OK
                  }
                  

                  這篇關于使用模板技巧訪問私人成員的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)

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

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

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

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

                          • 主站蜘蛛池模板: 国产高清视频 | 久久综合888| 亚洲二区视频 | 国产免费福利小视频 | 91在线视频免费观看 | 成人h动漫精品一区二区器材 | 欧美一区二区三区视频 | 黑人巨大精品欧美一区二区免费 | 女人精96xxx免费网站p | 精品视频久久久久久 | 亚洲欧美精品 | 99久久成人| 中文字幕日韩一区 | 亚洲欧美中文字幕在线观看 | 97精品超碰一区二区三区 | 国产精品美女久久久久久久久久久 | 91视频进入 | 999精品视频| 精品视频在线一区 | 91中文字幕在线观看 | 亚洲一av| 婷婷桃色网 | 亚洲国产成人久久久 | 久久国产精品99久久久大便 | 在线观看中文视频 | 亚洲精品国产综合区久久久久久久 | 中文字幕av一区 | 蜜桃av一区二区三区 | 久久y| 日韩欧美在线不卡 | 亚洲人人| 三级视频久久 | 成人免费视频网址 | 日韩看片 | 精品免费视频 | 国产精品自产av一区二区三区 | 久久国际精品 | 97高清国语自产拍 | 天天操妹子 | 看av网 | 日韩欧美亚洲 |