简体   繁体   English

访问周边的私有成员 class

[英]Accessing private members of surrounding class

I have two classes like this:我有两个这样的类:

#include <iostream>

class A {
    public:
        class B {
            public:
                void    printX(void) const { std::cout << A::x << std::endl; }
        };
    private:
        int x;
};

Obviously this piece of code doesn't work because B can't access x , but is there a way to make it work?显然这段代码不起作用,因为B无法访问x ,但是有没有办法让它起作用?

I've tried using friend class keyword in both classes like this:我试过在两个类中都使用friend class关键字,如下所示:

class A {
    public:
        class B {
            public:
            friend class A;
                void    printX(void) const { std::cout << A::x << std::endl; }
        };
        friend class B;
    private:
        int x;
};

But it didn't work either, and I can't figure out if it's even possible.但它也没有用,我什至不知道它是否可能。

According to the C++ 17 Standard (14.7 Nested classes)根据 C++ 17 标准(14.7 嵌套类)

1 A nested class is a member and as such has the same access rights as any other member. 1嵌套的 class 是一个成员,因此具有与任何其他成员相同的访问权限。 The members of an enclosing class have no special access to members of a nested class;封闭 class 的成员对嵌套 class 的成员没有特殊访问权; the usual access rules (Clause 14) shall be obeyed.应遵守通常的访问规则(第 14 条)。

The problem with the provided code is that x is not a static data member of the class A .提供的代码的问题是x不是 class A的 static 数据成员。 You need to provide an object of the class A the data member x of which will be accessed within an object of the nested class.您需要提供 class A的 object,其数据成员x将在嵌套的 class 的 object 中访问。

For example例如

    class A {
    public:
        class B {
        public:
            void    printX( const A &a ) const { std::cout << a.x << std::endl; }
        };
    private:
        int x;
    };

    A::B().printX( A() );

As the error message should tell you, A::x isn't a static member so you need an object instance to access it.正如错误消息应该告诉您的那样, A::x不是 static 成员,因此您需要一个object 实例来访问它。 If you add a reference to instance of A to B::A , you can use that to access A::x .如果将对A实例的引用添加到B::A ,则可以使用它来访问A::x

For example, the following works:例如,以下作品:

class A {
    public:
        class B {
            public:
                B(A const& a) : a(a) {}
                void printX(void) const { std::cout << a.x << std::endl; }

            private:
                A const& a;
        };
    private:
        int x;
};

Note that using a reference member has several implications.请注意,使用引用成员有几个含义。 Notably, you can now no longer reassign instances of type A::B , nor are its instances movable.值得注意的是,您现在不能再重新分配类型A::B的实例,其实例也不可移动。 As a consequence, it's often convenient to hold a pointer rather than a reference to A inside A::B .因此,在A::B中保存一个指针而不是对A的引用通常更方便。 Either way, you'll need to ensure that instances of A::B do not outlive the A instance they refer to, otherwise you end up with a dangling reference.无论哪种方式,您都需要确保A::B的实例不会超过它们引用的A实例,否则您最终会得到一个悬空引用。

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

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