Pass class object by reference and call it

Using this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  class A {
  
  public:
      function1();
      function2();
//---------------------- 
 
  int main() {
       A aObject;
       aObject.function();
  }

  int test() {
     aObject.function2();
  }


How do I use the same object from the main in the test function? I know I can do it in 2 way: make the object universal which is a bad idea, or pass it by reference. I'm not sure how to do that tho. Thanks!
Not sure why this was reported.

make the object universal

The correct term is global.

pass it by reference

http://www.cplusplus.com/doc/tutorial/functions/
See the section titled: Arguments passed by value and by reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A 
{
public:
      function1();
      function2();
}; 

int test (A & aobj);  //  Function prototype required
 
int main() 
{  A aObject;
    aObject.function();  //  There is no "function" in aObject. 
    test (aObject);
}

int test (A & aobj)   // pass by reference
{  aobj.function2();
}

Cool. Thanks a ton man!
Topic archived. No new replies allowed.