为什么受保护的构造函数引发错误这个代码? [英] Why is protected constructor raising an error this this code?
问题描述
有关受保护的构造函数的一个问题。我得知受保护的构造函数可以在派生类中使用。但是,我发现下面的代码有一个错误。为什么会发生这种情况?
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& $ c)的指针访问相同的成员$ c>)。
考虑以下示例
{
指针类型
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 $ c $
当应用于构造函数时,
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 classB
to access the contents of an object of base classA
only when that object of classA
is a subobject of classB
. That means that the only thing you can do in your code is to access the contents ofA
throughB
: you can access the members ofA
through a pointer of typeB *
(or a reference of typeB &
). But you cannot access the same members through a pointer of typeA *
(or referenceA &
).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 memberA::i
by using just plaini
syntax. This is equivalent to usingthis->i
syntax. Both will work, because the pointerthis
has typeB *
, i.e. you are accessingA::i
thorough a pointer of typeB *
. This is exactly what theprotected
access specifier is supposed to allow. The access throughpb
pointer works for the very same reason.However, when you "convert"
this
pointer to typeA *
, you can no longer accessA::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屋!