I am trying to figure out how to make a private/protected
member of an inner type accessible to its enclosing class.
My search led me to many questions about how to access an enclosing member from the inner class (e.g.) but this is not what I need. Many other posts traverse Java, and this is definitely not what I need. This is C++.
Consider this 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;
}
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:
$ 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
? Did I miss any silly detail?
Thanks in advance.