Hi,
the friend declaration does only say that B has access to A's private and protected elements.
But an instance of B does not know in which instance of A it should change the value of state. You could have something like this
there are three objects of type A, which one do you want to change ?
One possible solution would be giving B's update a parameter
Code:
class B{
public:
void update(A& a){
a.state = 1;
}
};
and using it this way
Code:
void loop() {
// put your main code here, to run repeatedly:
first.update();
second.update(first);
}
Much better C++ would be giving A a changeState method, instead of using friend.
Lesezeichen