class X
{
int Xi;
class Y
{
int Yi;
void func()
{
X x;
x.Xi = 5;
}
};
void func()
{
Y y;
y.Yi = 5;
// ^^^^ 'X::Y::Yi': cannot access private member declared in class 'X::Y'
}
};
I was learning about the Memento pattern and in the book I read, it was stated that one of the ways of achieving the pattern is by writing the Memento
class inside the Originator
class so that only the Originator
can have access to the private members of the Memento
class. When I tried to apply this method I got an error telling me that the private member is inaccessible. I know that I can use the keyword friend
and that will give me access to the private members. Also I know that I can access the outer class's private members from the inner class. But why can't the inner class access the private members of the inner class?
I can do this in java for example:
public class X {
int Xi;
public class Y
{
private int Yi;
public void func()
{
X x = new X();
x.Xi = 5;
}
}
public void func()
{
Y y = new Y();
y.Yi = 5;
}
}
Why is it not doable in C++?