Hi, I have been searching a lot for this one, but somehow I just can't get it right..
I want to store a reference to an object inside another object. In my example, I store the first class in the second and changes a variable in the main. But when I print the variable of the object in the second class afterward, it has not changed.
My setup is as following..
The main:
#include "mySecondClass.h"
#include "myClass.h"
myClass obj;
mySecondClass obj2;
void setup() {
Serial.begin(9600);
obj.testN = 5;
obj2.setObj(obj);
obj.testN = 10;
Serial.println(obj2.myClassObj.testN);
}
the first class header
// myClass.h
#ifndef _MYCLASS_h
#define _MYCLASS_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class myClass
{
protected:
public:
void init();
int testN = 0;
};
extern myClass my;
#endif
the second class header
// mySecondClass.h
#include "myClass.h"
#ifndef _MYSECONDCLASS_h
#define _MYSECONDCLASS_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class mySecondClass
{
protected:
public:
void init();
void setObj(myClass &obj);
myClass &myClassObj;
};
extern mySecondClass mySecond;
#endif
the second class cpp
#include "mySecondClass.h"
#include "myClass.h"
void mySecondClass::init()
{
}
void mySecondClass::setObj(myClass &obj)
{
myClassObj = obj;
}
mySecondClass mySecond;