C ++中的嵌套类访问控制 [英] nested class access control in C++

查看:191
本文介绍了C ++中的嵌套类访问控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内部类可以访问其封闭类的私有成员变量吗?互联网和编译器似乎有一些矛盾。编译(g ++在cygwin)允许它。

Can inner class access a private member variable of its enclosing class? There seems to be some contradictions on the internet and compiler. Compile (g++ on cygwin) allows it. But, some technical documents say that it is not allowed.

推荐答案

此问题已在缺陷报告#10 缺陷报告#45

原始和目前的C ++语言标准不会授予嵌套类任何额外的访问权限,即在访问封闭类的成员时没有任何权限。嵌套类只是普通的完全独立的类,它只是在一些其他类中声明。

The original and the current C++ language standard does not grant the nested class any extra access rights, i.e. it has no privileges when accessing members of the enclosing class. Nested classes are just ordinary completely independent classes, which just happen to be declared inside some other class.

但是根据缺陷报告45中提出的解决方案,嵌套类应该给予封闭类的成员完全访问权限。也就是说嵌套类本身应该被视为封闭类的成员,并且应该享有通常授予成员的所有访问权限。

But according to the proposed resolution in the Defect Report #45, the nested class should be given full access rights to the members of the enclosing class. I.e. the nested class should itself be considered a member of the enclosing class and should enjoy all access rights normally granted to members.

上次我检查了,这个缺陷的工作是尚未完成。一些编译器已经实现了他们认为新规范将需要的,即对嵌套类的完全访问权限(参见DR#45的建议解决方案)。一些编译器坚持当前标准的字母。这就是为什么你可能仍然观察到在不同的编译器之间在授予嵌套类的访问权限方面的一些不一致。

Last time I checked, the work on this defect was not finalized yet. Some compilers already implement whet they think the new specification will require, i.e. full access rights to nested classes (see the proposed resolution to the DR#45). Some compilers stick to the letter of the current standard. This is why you might still observe some inconsistency between different compilers with regard to access rights granted to nested classes.

这篇关于C ++中的嵌套类访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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