C ++嵌套类 - 内部/外部关系 [英] C++ nested classes - inner/outer relationship

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

问题描述

我在我们的社区和外部阅读了一些有关嵌套类的帖子,我很困惑。



据我所知,在 C ++,嵌套类中与单独的/独立的类没有任何区别。



在尝试更好地理解conecpt时,一个简单的代码,我发现一个内部类可以访问一个外部类,而没有得到友谊从外部类。



例如: / em>

  class Outer {
private://默认访问修饰符
int X;
static int sx;
public:
class Inner {
public:
void changeOuterDataMemberValues(int value){
sx = value; //更改Outer的私有静态数据成员。

外出
out.x = value; //通过对象改变私有数据成员(不通过类!)
}
void printMyOuterDataMember(){
cout< sx; //打印Outer的私有数据成员。
}
};
};


类Lonesome {
void attemptToChangeDataMemberValue(int value){
Outer :: sx = value; //不能更改Outer的私有静态数据成员。
}
};

int Outer :: sx;

您可以看到嵌套在Outer类中的Inner类可以访问它类)数据成员,而Lonesome作为独立类不能访问外部类数据成员。



如果这是一个重复或愚蠢的问题,我表示歉意,与你们说,嵌套类和独立类(两个不具有内部/外部关系的不同类)之间有区别。



谢谢大家,
Syndicator =]

解决方案

C ++ 03和C + +11在这方面。因此,答案因您使用的编译器而异。



如果您使用的是符合C ++ 03的编译器,则





如果您使用的是符合C ++ 11的编译器,则

$ < b
$ b

嵌套类可以访问封闭类的所有成员。

C ++ 03 Standard 11.8嵌套类:

§1


嵌套类的成员对封闭类的成员没有特殊访问权限,授予友谊的类或职能;

C ++ 11 Standard 11.7嵌套类:

>


嵌套类是一个成员,因此具有与任何其他成员相同的访问权限。



I read some posts about Nested Classes in our community and outside and I'm pretty confused.

As far as I understand, in C++, Nested Classes aren't not any different from separate/independent classes.

While I was trying to understand the conecpt better I wrote a simple code and I found out that an inner class can access an outer class without being given friendship from the outer class.

For example:

class Outer {
private : // default access modifier
    int x;
    static int sx;
public:
    class Inner {
    public:
        void changeOuterDataMemberValues(int value) {
            sx = value; // changes the private static data member of Outer.

            Outer out;
            out.x = value; // changes the private data member via object (not via class!)
        }
        void printMyOuterDataMember()  {
            cout << sx; // prints the private data member of Outer.
        }
    };
};


class Lonesome {
    void tryingToChangeDataMemberValue(int value) {
        Outer::sx = value; // cannot change the private static data member of Outer.
    }
};

int Outer::sx;

You can see that the Inner class which is nested in the Outer class has access to its(the Outer class) data members whilst the Lonesome as independent class cannot access the Outer class data member.

I apologize if this is a duplicate or stupid question, but I just want to confirm with you guys that there is a difference between a Nested Class and independent class (two different classes which don't have inner / outer relationship).

Thank you all, Syndicator =]

解决方案

There is a difference between C++03 and C++11 in this regards. So the answer varies depending on which compiler you are using.

If you are using a C++03 compliant compiler then:

Nested class cannot access all members of the enclosing class.

If you are using C++11 compliant compiler then:

Nested class can access all members of the enclosing class. Nested class is treated as just another member of the class.

C++03 Standard 11.8 Nested classes:
§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 friendship to an enclosing class; the usual access rules shall be obeyed.

C++11 Standard 11.7 Nested Classes:

A nested class is a member and as such has the same access rights as any other member.

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

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