class by ref

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.

hope that makes some sense.

No, it doesn't.

this is the bigger object that i want to be passed the test object as a reference.

Then why is theTest's type Test?

You should be using pointers. theTest should be defined like so:
Test *theTest; // currently points to nothing

Then, theTest can be made to point to _theTest, with also should be a Test *.

Then, you can call the constructor with the address of (not a reference to) an object.

  //setup the test object
  Test theTest = Test();

Nonsense. You NEVER call the constructor directly. That should be:

  //setup the test object
  Test theTest;
  //put the object inside another object
  TestContainer tc = TestContainer(theTest);

should be:

  //put the object inside another object
  TestContainer tc = TestContainer(&theTest);

after you make the TestContainer constructor take a pointer.

class TestContainer{
  public:
  Test & theTest;

  TestContainer(Test &_theTest) : theTest ( _theTest) {}
};

PaulS has some good points, but you can pass by reference. As he says, construct it like this:

  //setup the test object
  Test theTest;

Output:

1 theTest.i : 0
2 theTest.i : 1
3 tc.theTest.i : 1
4 theTest.i : 2
5 tc.theTest.i : 2

Thanks alot to both of you.

sorry it didnt make sense.

I can see some silly stuff ive done there, but i can also how i can get the reference/address of passed.

thanks

stuart

sjeffrey:
I can see some silly stuff ive done there, but i can also how i can get the reference/address of passed.

Oh, aye.