iciwi
27.08.2016, 10:25
Hey there,
I have a problem: I want to change the member of a class A from class B. So I made class B to a friend of class A. There is a function in class B that changes the variable "state". And in class A there is a function that checks if the variable "state" is 1. If true it will turn on an Led. I wrote this code:
class A {
friend class B;
private:
int state;
int Led = 13;
A(){
pinMode(Led, OUTPUT);
}
void update(){
if (state == 1){
digitalWrite (Led, HIGH);
}
}
};
class B{
public:
void update(){
state = 1;
}
};
A first;
B second;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
first.update();
second.update();
}
But it doesnt work. Where is the mistake? I hope somebody can help me.
Best regards
Maurice W.
I have a problem: I want to change the member of a class A from class B. So I made class B to a friend of class A. There is a function in class B that changes the variable "state". And in class A there is a function that checks if the variable "state" is 1. If true it will turn on an Led. I wrote this code:
class A {
friend class B;
private:
int state;
int Led = 13;
A(){
pinMode(Led, OUTPUT);
}
void update(){
if (state == 1){
digitalWrite (Led, HIGH);
}
}
};
class B{
public:
void update(){
state = 1;
}
};
A first;
B second;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
first.update();
second.update();
}
But it doesnt work. Where is the mistake? I hope somebody can help me.
Best regards
Maurice W.