无法从派生类型的范围访问另一个实例的受保护成员 [英] Cannot access protected member of another instance from derived type's scope
问题描述
在此问题的答案中,"为什么我的对象无法访问定义的另一个对象的受保护成员"在普通的基类中?",可以读到:
In this answer to the question "Why can't my object access protected members of another object defined in common base class?", one can read:
您只能从自己的基类实例访问受保护的成员.
You can only access protected members from your own base class instance.
我无法正确获取它,或者跟随MCVE(在coliru上运行)证明是错误的:
Either I don't get it correctly or the following MCVE (live on coliru) proves it wrong:
struct Base { void f(); protected: int prot; };
struct Derived : Base { void g(); private: int priv; };
void Base::f()
{
Base b;
b.prot = prot;
(void) b;
}
void Derived::g()
{
{
Derived d;
(void) d.priv;
}
{
Derived& d = *this;
(void) d.priv;
}
{
Derived d;
(void) d.prot; // <-- access to other instance's protected member
}
{
Derived& d = *this;
(void) d.prot;
}
// ---
{
Base b;
(void) b.prot; // error: 'int Base::prot' is protected within this context
}
{
Base& b = *this;
(void) b.prot; // error: 'int Base::prot' is protected within this context
}
}
鉴于这两个错误,我想知道:为什么我可以从Derived
的范围访问另一个Derived
实例的受保护成员,但是无论如何都不能从相同的范围访问另一个Base
实例的受保护成员. Derived
源自Base
的事实? Tl; dr:在这种情况下,是什么使protected
比private
更私有"?
In the light of the two errors I get to wonder: why can I access to another Derived
instance's protected member from the scope of Derived
but cannot access to another Base
instance's protected member from the same scope regardless of the fact that Derived
devires from Base
? Tl; dr: what makes protected
more "private" than private
in this case?
注释:
- 请不要关闭该问题,不要重复链接该问题;
- 欢迎使用更好的标题建议.
推荐答案
如果在类
N
中命名为[...]
A member
m
is accessible at the point R when named in classN
if [...]
-
作为
-
m
受保护,并且 R 出现在类N
的成员或朋友中,或者出现在从<派生的类P
的成员中c8>,其中m
作为P
的成员是public
,private
或protected
N
成员的m
as a member ofN
is protected, and R occurs in a member or friend of classN
, or in a member of a classP
derived fromN
, wherem
as a member ofP
ispublic
,private
, orprotected
里面有很多字母.但是基本上有两个条件:
There's a lot of letters in there. But there are basically two conditions:
-
R
是该课程的成员或朋友.这处理了d.prot
的示例-访问Derived
的受保护成员时,我们位于Derived
的成员中. -
R
在派生类的成员中,被访问的成员是派生类实例的的成员.这处理了b.prot
示例-我们在派生类的成员中,但是prot
不是派生类的成员.
R
is in a member or friend of the class. This handles thed.prot
example - we are in a member ofDerived
while accessing a protected member ofDerived
.R
is in a member of a derived class and the member being accessed is a member of the derived class instance. This handles theb.prot
example - we are in a member of a derived class, butprot
is not a member of the derived class.
换句话说,Derived
可以访问Base
的受保护成员-但仅在它正在访问其自己的子对象的受保护成员的情况下.它无法访问其他Base
对象的受保护成员.当您认为另一个Base
很容易成为SomeOtherDerived
时,这很有意义,在这种情况下,这对我们来说是另一个无关的对象,我们没有特殊的访问权限.
In other words, Derived
can access Base
's protected members - but only in the case that it is accessing its own subobject's protected members. It cannot access other Base
object's protected members. This makes sense when you consider that this other Base
could easily be SomeOtherDerived
, in which case that's just another unrelated object to us that we have no special access privileges to.
这篇关于无法从派生类型的范围访问另一个实例的受保护成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!