C ++嵌套类可访问性 [英] C++ nested classes accessibility

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

问题描述

以下代码不考虑两个类之间的友谊

  class OutSideClass 
{
...
public:
int i_pub;
protected:
int i_pro;
private:
int i_pri;

class InSideClass
{
...
public:
int j_pub;
protected:
int j_pro;
private:
int j_pri;
};
};问题1>是否真的OutSideClass只能访问InSideClass的公共成员

$ b



$ b

问题2> InSideClass是否可以访问OutSideClass的所有成员



如果我的理解不正确,请纠正我。问题1> OutSideClass是否只能访问InSideClass的公共成员

$

b $ b


问题2> InSideClass是否访问OutSideClass的所有成员


否,在C ++ 03中。是的,在C ++ 11中。






标准文本对此非常清楚:



C ++标准(2003)在$ 11.8 / 1 [class.access.nest]中说,


strong>嵌套类的成员对
封闭类的成员没有特殊访问权限,也没有授予
友谊给类封装类的类或函数;应遵守通常的访问规则(第11节)
封闭类的成员没有特殊的
访问嵌套类的成员;


但是,标准报价只有一个缺陷。它说嵌套类不能访问封闭类的私有成员。但在C ++ 11中,它已被更正:在C ++ 11中,嵌套类可以访问封闭类的私有成员(虽然封闭类仍然无法访问嵌套类的私有成员)。



查看此缺陷报告:




Given the following code without considering friendship between two classes:

class OutSideClass
{
...
public:
    int i_pub;
protected:
    int i_pro;
private:
    int i_pri;

    class InSideClass
    {
        ...
        public:
            int j_pub;
        protected:
            int j_pro;
        private:
            int j_pri;
    };
};

Question 1> Is it true that OutSideClass can ONLY access public members of InSideClass

Question 2> Is it true that InSideClass can access all members of OutSideClass

Please correct me if my understanding is not correct.

解决方案

Question 1> Is it true that OutSideClass can ONLY access public members of InSideClass

Yes

Question 2> Is it true that InSideClass can access all members of OutSideClass

No, in C++03. Yes, in C++11.


The Standard text is very clear about this:

The C++ Standard (2003) says in $11.8/1 [class.access.nest],

The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be obeyed. The members of an enclosing class have no special access to members of a nested class; the usual access rules (clause 11) shall be obeyed.

However, the Standard quotation has one defect. It says the nested classes don't have access to private members of the enclosing class. But in C++11, it has been corrected: in C++11, nested classes do have access to private members of the enclosing class (though the enclosing class still doesn't have access to private members of the nested classes).

See this Defect Report :

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

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