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

Why is protected constructor raising an error this this code?(为什么受保护的构造函数会引发此代码的错误?)

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

问题描述

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

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

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

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

基础教程推荐