简体   繁体   English

内部类私人成员的访问和圈中人士的友善

[英]Inner class private member access and enclosing's friendness

I am trying to figure out how to make a private/protected member of an inner type accessible to its enclosing class. 我试图弄清楚如何使内部类型的private/protected成员可以对其封闭的类访问。

My search led me to many questions about how to access an enclosing member from the inner class ( eg ) but this is not what I need. 我的搜索使我想到了许多有关如何从内部类访问封闭成员的问题( 例如 ),但这不是我所需要的。 Many other posts traverse Java, and this is definitely not what I need. 其他许多文章遍历Java,这绝对不是我所需要的。 This is C++. 这是C ++。

Consider this MCVE: 考虑以下MCVE:

#include <iostream>

struct bla
{
    bla( );
    ~bla( );

    class inner;

    inner *_inner;
};

class bla::inner
{
public:
    inner( )
    :   field( 13 )
    {}

private:
    friend struct bla;

    unsigned field;
};


bla::bla( )
:   _inner( new inner )
{}

bla::~bla( )
{
    delete _inner;
}

int main( )
{
    bla _bla;
    _bla._inner->field = 42;
    std::cout << "bla::_inner->field: " << _bla._inner->field << std::endl;
}

Live example 现场例子

The only question I could find out treating what I need is C++ Outer class access Inner class's private - why forbidden , but the proposed solution does not work for me: 我能找到的唯一问题是,我需要C ++外部类访问内部类的private-为什么禁止使用 ,但是建议的解决方案对我不起作用:

$ g++ -std=c++14 -Wall inner_class.cpp -o inner_class
inner_class.cpp: In function ‘int main()’:
inner_class.cpp:23:11: error: ‘unsigned int bla::inner::field’ is private
  unsigned field;
           ^
inner_class.cpp:39:15: error: within this context
  _bla._inner->field = 42;
               ^
inner_class.cpp:23:11: error: ‘unsigned int bla::inner::field’ is private
  unsigned field;
           ^
inner_class.cpp:40:54: error: within this context
  std::cout << "bla::_inner->field: " << _bla._inner->field << std::endl;

How to properly give bla access to field ? 如何正确地让bla进入field Did I miss any silly detail? 我错过了任何愚蠢的细节吗?

Thanks in advance. 提前致谢。

Your code is correctly granting the friendship required for bla to access the private members of bla::inner . 您的代码正确授予了bla访问bla::inner的私有成员所需的友谊。 However, your problem is that you're not accessing the private member in bla (which would have access), you're accessing it in main , which of course has no access. 但是,您的问题是您没有访问bla的私有成员(该成员可以访问),而是在main访问它,而后者当然没有访问权限。

If you want bla to be able to access bla::inner::field , you need to give bla some functions for accessing it. 如果您希望bla能够访问bla::inner::field ,则需要为bla一些访问它的功能。 Example: 例:

struct bla
{
    bla( );
    ~bla( );

    class inner;

    inner *_inner;

    void setInnerField(int val) { _inner->field = val; }
};

// The rest as before

int main( )
{
    bla _bla;
    _bla.setInnerField(42);
    std::cout << "bla::_inner->field: " << _bla._inner->field << std::endl;
}

If, on the other hand, you want to give all outside code (such as main ) access to the field, just make it public. 另一方面,如果您想让所有外部代码(例如main )访问该字段,只需将其公开。

You can't. 你不能

You seem to think that because _bla is an object of bla and bla is the outer class that you can access private fields of inner outside of bla . 您似乎认为,因为_blabla的对象,而bla是可以访问bla inner外部的私有字段的外部类。 This is not the case. 不是这种情况。

Access modifiers work on scope, not on the objects you're operating on. 访问修饰符在作用域上起作用,而不在操作对象上起作用。 You can access the private fields of inner in the class body of bla (in its methods etc). 您可以在bla的类主体中访问inner的私有字段(在其方法等中)。 Not outside of it. 不在外面。

Making main as friend of bla::inner should work. 成为bla :: inner的朋友应该主要。

    class bla::inner
{
public:
    inner( )
    :   field( 13 )
    {}

private:
    friend struct bla;
    friend int main(); // Make main friend of bla
    unsigned field;
};

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

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