为什么受保护的构造函数引发错误这个代码? [英] Why is protected constructor raising an error this this code?

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

问题描述

有关受保护的构造函数的一个问题。我得知受保护的构造函数可以在派生类中使用。但是,我发现下面的代码有一个错误。为什么会发生这种情况?

  class A 
{
protected:
A ){}
};

class B:public A {
public:
B(){
A * f = new A(); //为什么它不工作在这里
}
};


解决方案

这与构造函数无关。这是受保护访问的工作原理。



方法 protected 访问指定器工作,它允许派生类 B 访问基类 A <只有当 A 类的对象是 B 类的子对象时才会返回。这意味着你在代码中唯一可以做的是访问 A B :您可以通过类型 B * 的指针访问 A 的成员类型 B& )。但您不能通过类型 A * (或引用 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 语法。两个都会工作,因为指针这有类型 B * ,即你正在访问 A :: i 通过类型 B * 的指针。这正是 protected 访问说明符应该允许的。通过 pb 指针的访问也是同样的原因。



$ c> 这个指针类型 A * ,你不能再访问 A :: i

当应用于构造函数时, protected 访问说明符具有非常特殊的效果:受保护的构造函数只能用于初始化基类子对象。它不能用于初始化独立对象(这是你想要做的)。换句话说,受保护的构造函数是另一种在C ++中实现抽象类概念的方式(以及纯虚方法)。如果你的类的构造函数是受保护的,那么你的类是有效的 abstract 。您不能使用它从外部定义独立对象。 (当然,以上不适用于朋友,以及在类本身内)。


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
        }
};

解决方案

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

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

Consider the following example

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
  }
};

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.

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.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆