访问内部类的封闭成员和内部成员 [英] Accessing enclosing and inner members of inner class

查看:79
本文介绍了访问内部类的封闭成员和内部成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在这里和那里的INTERNET上冲浪,在C ++中寻找实习生类概念的概念.

有人说gcc允许内部类访问封闭类的成员,而MVS编译器拒绝它.

那么人们,哪个是正确的?



11.8嵌套类[class.access.nest]

1嵌套类的成员没有对
成员的特殊访问权限 封闭类,也不允许封闭已授予好友的类或函数-
运送到封闭的舱室;常规访问规则(_class.access_)
应当服从.封闭类的成员没有特殊的
访问嵌套类的成员;常用的访问规则
(_class.access_)应该服从.
[示例:]

Hi All,

I was surfing in the INTERNET here and there looking for the concept of the intern class concept in the C++.

Some people say that the gcc allow the inner class accessing the members of the enclosing class whereas the MVS compiler reject it.

So people, which one is correct ?



11.8 Nested classes [class.access.nest]

1 The members of a nested class have no special access to members of an
enclosing class, nor to classes or functions that have granted friend-
ship to an enclosing class; the usual access rules (_class.access_)
shall be obeyed. The members of an enclosing class have no special
access to members of a nested class; the usual access rules
(_class.access_) shall be obeyed.
[Example:]

class E {
    int x;
    class B { };

    class I {
        B b;            // error: E::B is private
        int y;
        void f(E* p, int i)
        {
            p->x = i;   // error: E::x is private
        }
    };
    int g(I* p)
    {
        return p->y;    // error: I::y is private
    }
};


[-最终示例]


[--end example]

推荐答案

在C ++中确实没有内部类的概念Java中的方式.

如果您声明一个嵌套类,则它对该封闭类的成员没有任何特权访问-它实际上不知道该封闭类在那儿.封闭的类无权访问嵌套类的成员-它就像另一个数据成员(如果您声明它和它的实例)一样,或者是另一个级别的命名空间作用域.

在C ++中,唯一一次需要嵌套类的时间就是您要建模两个概念,而另一个则是另一个实现细节.即便如此,还有更好的方法来对依赖关系进行建模-即,将要嵌套的类粘贴到封闭类的实现文件中.

因此,如果您想让嵌套类重新考虑的冲动,那实际上只会使您的代码更难阅读,并且与其他方法相比,不会改变代码的各个位之间的访问.

干杯,

Ash
There really isn''t a concept of an internal class in C++ the way there is in Java.

If you declare a nested class it doesn''t have any privileged access to members of the enclosing class - it effectively doesn''t know the enclosing class is there. The enclosing class doesn''t have any access to the members of the nested class - it''s either just like another data member (if you declare and instance of it) or another level of namespace scoping.

About the only time you need a nested class in C++ is when you''ve got two concepts to model and one''s an implementation detail of the other. And even then there are better ways of modelling the dependency - i.e. sticking the class you were going to nest in the enclosing class''s implementation file.

So if you ever feel the urge to nest classes think again, it really only makes your code harder to read and doesn''t change the access of the various bits of your code to each other compared to other methods.

Cheers,

Ash


所有编译器的C ++概念都应相同..rt??

应该是,是的.但实际上,编译器在诸如此类的事情上常常会有所不同.
C++ concepts should be same for all compilers..rt??

should be, yes. but in reality, compilers often differ on things like this.


这篇关于访问内部类的封闭成员和内部成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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