简体   繁体   English

继承层次结构中的成员访问-C ++

[英]Member access in Inheritance Hierarchy - C++

struct A {
protected:
  int y;
public:
  int z;
};

struct F : A {
public:
  using A::y;
private:
  using A::z;
};

int main() {
  F obj_F;
  obj_F.y = 9;
  obj_F.z = 10;
}

Source: 资源: http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc14cplr135.htm http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc14cplr135.htm

In the above code obj_F.z = 10; 在上面的代码中obj_F.z = 10; - is allowed. - 被允许。 Explanation: The access of member z is still public. 说明:成员z的访问仍然是公共的。 The private using declaration using A::z has no effect on the access of z. 使用A :: z的private using声明对z的访问没有影响。

Can someone tell me, if z, which is declared as private, is accessible outside, then what is the meaning of that private? 有人可以告诉我,如果声明为私有的z在外部可以访问,那么私有的含义是什么? What does it do? 它有什么作用?

Thanks 谢谢

-Saiyasodharan -Saiyasodharan

The code is valid according to the Standard - see this Standard rule, which I did not have in mind when I answered before 该代码根据标准是有效的 -请参阅此标准规则,我之前回答时并没有想到

A member m is accessible when named in class N if 如果在类N中命名,则成员m是可访问的,如果

  • [...], or [...], 要么
  • there exists a base class B of N that is accessible at the point of reference, and m is accessible when named in class B. 在参考点存在N的基类B,并且在类B中命名时可以访问m。

This entirely applies to your code, and thus the access is valid... It appears the main purpose of this rule is for allowing friend declarations of the base to apply to inherited members, but it also applies to this case. 这完全适用于您的代码,因此访问是有效的……该规则的主要目的似乎是允许基础的朋友声明适用于继承的成员,但也适用于此情况。


(Disregard the parts of this that say the code is invalid - It's valid as explained above. This part is an old version of my answer, kept here to serve as background information) (忽略代码无效的部分-如上所述,它是有效的。这部分是我的回答的旧版本,此处保留作为背景信息)

No, this code is invalid. 不,此代码无效。 That's why the equivalent "access-declarations" are called that way (these are deprecated though) 这就是为什么以这种方式调用等效的“访问声明”的原因(尽管已弃用)

struct F : A {
public:
  A::y;
private:
  A::z;
};

These are called "access-declarations" precisely because they can change the access... In your example the naming class is F and z as a member of F is private because the using-declaration did change the access level of the name z . 这些之所以称为“访问声明”,恰恰是因为它们可以更改访问权限...在您的示例中,命名类是F而作为F成员的z是私有的,因为using声明确实更改了名称z的访问级别。

F inherits A privately. F私下继承A。 So you cannot access any of A's public members if you use an F outside F's definition. 因此,如果在F的定义之外使用F,则无法访问A的任何公共成员。 Because F defines no members, F is basically useless. 因为F没有定义成员,所以F本质上是无用的。

If F inherits A publicly (ie :) 如果F公开继承A(即:)

struct F : public A {};

Then you can now use A's public member. 然后,您现在可以使用A的公共成员。 so you can write obj_F.z 所以你可以写obj_F.z

If you have a variable z that is public in a and private in F, ie : 如果变量z在a中是公共的,而在F中是私有的,即:

struct A { public: int z; };
struct F : public A { private: int z; };

then the two z are actually two different variables. 那么两个z实际上是两个不同的变量 That's not what you want. 那不是你想要的。

also, like Andre Holzner said, your using statement are useless here. 而且,就像安德烈·霍尔兹纳(Andre Holzner)所说的那样,您的using语句在这里没有用。

Generally, you cannot in any object programming language, restrict the scope of a member in a public-inherited subclass. 通常,您不能使用任何对象编程语言来限制公共继承的子类中成员的范围。 Either the class definition won't compile, won't obey, or will create another member with the same name. 类定义将不会编译,不会服从或将创建另一个具有相同名称的成员。 This because your F is not compatible with code that deals with a A or a subclass of it. 这是因为您的F与处理A或其子类的代码不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM