久久久久久久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>

                          • 主站蜘蛛池模板: 成人精品福利 | 国产在线视频网站 | 精品久久久久久久久久久 | 欧美日韩免费在线 | 视频一区二区在线 | 亚洲天堂一区二区三区 | 美女黄色在线观看 | 亚洲欧美日韩国产精品 | 久久精品视频一区二区 | 亚洲视频在线一区 | 欧美理论片在线观看 | 日韩精品一级毛片在线播放 | 日韩欧美视频在线 | 96精品| 韩日一区二区 | 超碰人人人 | 综合激情网| 成人福利视频 | av资源站 | 超碰在线国产 | 精久久久久 | 国产黄a三级三级看三级 | 国产精品午夜视频 | 一区视频在线 | 午夜av在线播放 | 欧美精品一二三 | 一道本在线观看 | 美日韩精品 | 午夜精品国产精品大乳美女 | 亚洲高清免费视频 | 成人国产一区 | 亚洲日本中文字幕 | 91亚洲国产成人久久精品网站 | 日日不卡av | 日韩免费在线 | 亚洲高清毛片一区二区 | 午夜视频在线播放 | 欧日韩av | 久久久91| 看黄色大片 | 国v精品久久久网 |