if you pass an object to a function only a copy will be passed and the object in the calling function remains unchanged,copies are created from the call stack which consumes memory with repeated calls.
if you instead pass a reference to an object then changes are reflected in the calling function >
Code:
#include <stdlib.h>
#include<stdio.h>
class TestClass
{
public:
int testvar_A;
int testvar_B;
};
void AnyFunction1(TestClass myobject)
{
myobject.testvar_A = 25;
myobject.testvar_B = 50;
printf("Now we are in AnyFunction, testvar_A = %d testvar_B = %d\n\n",myobject.testvar_A, myobject.testvar_B);
}
void AnyFunction2(TestClass* pMyobject)
{
pMyobject->testvar_A = 25;
pMyobject->testvar_B = 50;
printf("Now we are in AnyFunction, testvar_A = %d testvar_B = %d\n\n",pMyobject->testvar_A, pMyobject->testvar_B);
}
int main(int argc, char *argv[])
{
TestClass myobject;
myobject.testvar_A = 0;
myobject.testvar_B = 0;
printf("Now we are in main(), testvar_A = %d testvar_B = %d\n\n",myobject.testvar_A, myobject.testvar_B);
printf("Calling AnyFunction1.......\n");
AnyFunction1(myobject);
printf("Now we are back in main(), testvar_A = %d testvar_B = %d\n\n\",myobject.testvar_A, myobject.testvar_B);
printf("\n");
printf("** Show the same using pass by reference **\n\n");
printf("Now we are in main(), testvar_A = %d testvar_B = %d\n\n",myobject.testvar_A, myobject.testvar_B);
printf("Calling AnyFunction2.......\n");
AnyFunction2(&myobject);
printf("Now we are back in main(), testvar_A = %d testvar_B = %d\n\n",myobject.testvar_A, myobject.testvar_B);
system("PAUSE");
}