• <tfoot id='nazhf'></tfoot>

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

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

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

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

        为什么受保护的构造函数会引发此代码的错误?

        时间:2023-09-27
          <bdo id='3eUSC'></bdo><ul id='3eUSC'></ul>

          <small id='3eUSC'></small><noframes id='3eUSC'>

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

                • 本文介绍了为什么受保护的构造函数会引发此代码的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  关于受保护构造函数的一个问题.我了解到受保护的构造函数可以在派生类中使用.但是,我发现下面的代码有错误.为什么会这样?

                  One question about protected constructor. I learnt that the protected constructor can be used in the derived class. How ever, I found the code below has an error. Why does it happen like this?

                  class A
                  {
                      protected:
                          A(){}
                  };
                  
                  class B: public A {
                      public:
                          B() {
                              A* f=new A();           // Why it is not working here
                          }
                  };
                  

                  推荐答案

                  这与具体的构造函数无关.这就是 protected 访问的工作原理.

                  This has nothing to do with constructors specifically. This is just how protected access works.

                  protected 访问说明符的工作方式,它允许派生类B 访问基类A 的对象的内容仅当 A 类的对象是 B 类的子对象时.这意味着你在你的代码中唯一能做的就是访问AB的内容:你可以访问A的成员code>A 通过 B * 类型的指针(或 B & 类型的引用).但是您不能通过A *类型的指针(或引用A &)访问相同的成员.

                  The way protected access specifier works, it allows the derived class B to access the contents of an object of base class A only when that object of class A is a subobject of class B. That means that the only thing you can do in your code is to access the contents of A through B: you can access the members of A through a pointer of type B * (or a reference of type B &). But you cannot access the same members through a pointer of type A * (or reference A &).

                  看下面的例子

                  class A {
                  protected:
                    int i;
                  };
                  
                  class B : A  {
                    void foo() {
                      i = 0;        // OK
                      this->i = 0;  // OK
                  
                      B *pb = this;
                      pb->i = 0;    // OK
                  
                      A *pa = this;
                      pa->i = 0;    // ERROR
                  
                      ((A *) this)->i = 0; // ERROR
                    }
                  };
                  

                  在上面的 B::foo 中,您可以使用简单的 i 语法访问基本成员 A::i.这相当于使用 this->i 语法.两者都可以工作,因为指针 this 的类型为 B *,即您正在通过 类型的指针访问 A::iB *.这正是 protected 访问说明符应该允许的.通过 pb 指针访问的工作原理与此相同.

                  In the above B::foo, you can access base member A::i by using just plain i syntax. This is equivalent to using this->i syntax. Both will work, because the pointer this has type B *, i.e. you are accessing A::i thorough a pointer of type B *. This is exactly what the protected access specifier is supposed to allow. The access through pb pointer works for the very same reason.

                  但是,当您转换"this 指针为类型 A * 时,您将无法再通过该新指针访问 A::i指针,即使您仍在尝试访问它们与以前完全相同的成员.

                  However, when you "convert" this pointer to type A *, you can no longer access A::i through that new pointer, even though you are still trying to access they very same member as before.

                  当应用于构造函数时,protected 访问说明符具有非常特殊的效果:受保护的构造函数只能用于初始化基类子对象.它不能用于初始化独立对象(这是您尝试执行的操作).换句话说,受保护的构造函数是在 C++ 中实现抽象类概念的另一种方式(以及纯虚方法).如果您的类的构造函数受到保护,那么您的类实际上是抽象.您不能使用它来从外部"定义独立对象.(当然,以上不适用于朋友之间,也不适用于班级本身).

                  When applied to constructors, the protected access specifier has a very specific effect: a protected constructor can only be used to initialize base-class subobjects. It cannot be used to initialize standalone objects (which is what you were trying to do). In other words, protected constructors are another way to implement the concept of abstract class in C++ (along with pure virtual methods). If the constructors of your class are protected, then your class is effectively abstract. You can't use it to define independent objects "from outside". (Of course, the above does not apply within friends, as well as within the class itself).

                  这篇关于为什么受保护的构造函数会引发此代码的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:避免繁琐的可选参数 下一篇:为什么复制构造函数参数是常量?

                  相关文章

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

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

                • <legend id='UzlzZ'><style id='UzlzZ'><dir id='UzlzZ'><q id='UzlzZ'></q></dir></style></legend>

                      <tfoot id='UzlzZ'></tfoot>
                        <bdo id='UzlzZ'></bdo><ul id='UzlzZ'></ul>