• <bdo id='WDvYl'></bdo><ul id='WDvYl'></ul>
  1. <legend id='WDvYl'><style id='WDvYl'><dir id='WDvYl'><q id='WDvYl'></q></dir></style></legend>

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

      <tfoot id='WDvYl'></tfoot>

      使用模板访问 C++ 中超类的受保护成员

      时间:2023-05-25

      • <small id='zRSGj'></small><noframes id='zRSGj'>

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

          <tfoot id='zRSGj'></tfoot>
              • <bdo id='zRSGj'></bdo><ul id='zRSGj'></ul>
                <legend id='zRSGj'><style id='zRSGj'><dir id='zRSGj'><q id='zRSGj'></q></dir></style></legend>
                本文介绍了使用模板访问 C++ 中超类的受保护成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                为什么 C++ 编译器不能识别 g()bSuperclass 的继承成员,如以下代码所示:

                Why can't a C++ compiler recognize that g() and b are inherited members of Superclass as seen in this code:

                template<typename T> struct Superclass {
                 protected:
                  int b;
                  void g() {}
                };
                
                template<typename T> struct Subclass : public Superclass<T> {
                  void f() {
                    g(); // compiler error: uncategorized
                    b = 3; // compiler error: unrecognized
                  }
                };
                

                如果我简化 Subclass 并且只是从 Subclass 继承然后它编译.当将 g() 完全限定为 Superclass::g()Superclass::b 时,它也会编译.我使用的是 LLVM GCC 4.2.

                If I simplify Subclass and just inherit from Subclass<int> then it compiles. It also compiles when fully qualifying g() as Superclass<T>::g() and Superclass<T>::b. I'm using LLVM GCC 4.2.

                注意:如果我在超类中公开 g()b ,它仍然会失败并出现相同的错误.

                Note: If I make g() and b public in the superclass it still fails with same error.

                推荐答案

                这可以通过使用 using 将名称拉入当前范围来修改:

                This can be amended by pulling the names into the current scope using using:

                template<typename T> struct Subclass : public Superclass<T> {
                  using Superclass<T>::b;
                  using Superclass<T>::g;
                
                  void f() {
                    g();
                    b = 3;
                  }
                };
                

                或者通过this指针访问来限定名称:

                Or by qualifying the name via the this pointer access:

                template<typename T> struct Subclass : public Superclass<T> {
                  void f() {
                    this->g();
                    this->b = 3;
                  }
                };
                

                或者,正如您已经注意到的,通过限定全名.

                Or, as you’ve already noticed, by qualifying the full name.

                之所以有必要这样做,是因为 C++ 不考虑用于名称解析的超类模板(因为它们是从属名称并且不考虑从属名称).它在您使用 Superclass 时有效,因为它不是模板(它是模板的实例化),因此它的嵌套名称不依赖 名字.

                The reason why this is necessary is that C++ doesn’t consider superclass templates for name resolution (because then they are dependent names and dependent names are not considered). It works when you use Superclass<int> because that’s not a template (it’s an instantiation of a template) and thus its nested names are not dependent names.

                这篇关于使用模板访问 C++ 中超类的受保护成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C++ 模板类型名迭代器 下一篇:c++ 模板类;具有任意容器类型的函数,如何定义

                相关文章

                最新文章

                <small id='8UEZj'></small><noframes id='8UEZj'>

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

                  <tfoot id='8UEZj'></tfoot>

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