Hi.
im struggling to get an object to pass into another object by reference rather then value. in the main setup i want to construct my first object, then pass it to the second object and it be the same object when i reference it later.
hope that makes some sense.
i have produced this simplified version to kind of show what i mean
/*
Test
the object that will be passed and i want to be the same object reference
it has and integer and a method to add 1 to the integer
*/
class Test{
public:
int i;
Test(){
i=0;
}
void addone(){
i=i+1;
}
};
/*
Test Container
this is the bigger object that i want to be passed the test object as a reference.
*/
class TestContainer{
public:
Test theTest;
//i thought the & should make it ref
TestContainer(Test &_theTest)
{
//i've tried an & here but wont compile
theTest = _theTest ;
}
};
void setup(){
Serial.begin(57600);
//setup the test object
Test theTest = Test();
//print its value - should be 0
Serial.print("1 theTest.i : ");
Serial.println(theTest.i);
//add 1 to the value
theTest.addone();
//print it, it should now be 2
Serial.print("2 theTest.i : ");
Serial.println(theTest.i);
//put the object inside another object
TestContainer tc = TestContainer(theTest);
//test to see its value is the same as before
Serial.print("3 tc.theTest.i : ");
Serial.println(tc.theTest.i);
//add 1 to the test object
theTest.addone();
//test to make sure its added 1
Serial.print("4 theTest.i : ");
Serial.println(theTest.i);
//make sure the container object also is the same.
//for some reason its not
Serial.print("5 tc.theTest.i : ");
Serial.println(tc.theTest.i);
Serial.println();
}
void loop(){
}
i just need to know how i can pass theTest to tc and it be the same object
thanks for any help.