After reading some of your previous posts, I think you are basically asking:
Why do we pass in and out pointers sometimes, whereas other time we pass in and out normal variable?
Whenever you pass any type of variable into a function, be it a pointer, a class or a normal variable, the function you pass it to never gets the original, it just gets a copy which gets destroyed at the end of the function. For example :
Code:
void main()
{
int nTest = 10;
ExampleFunction(nTest);
cout << nTest;
}
void ExampleFunction(int nInput)
{
nInput += 10;
}
In the preceding example, the nTest variable that gets outputted to the screen at the end of main() equals its original value of 10, even though it was passed into ExampleFunction, which adds 10 to it. This is because ExampleFunction works with a copy of nTest, adds 10 to it, which then it gets forgotten at the end of ExampleFunction. However, if you have the following situation:
Code:
void main()
{
int nTest = 10;
ExampleFunction(&nTest);
cout << nTest;
}
void ExampleFunction(int* pnInput)
{
*pnInput += 10;
}
In the preceding example, ExampleFunction now takes in a pointer to an integer. Remember in my previous post I explained that pointers just hold a 32 bit (8 byte) number representing a position in memory? Well, that is what ExampleFunction expects to be given, a position in memory. In the main() function nTest is declared as an integer equaling 10, but we don't want to pass that to the ExampleFunction any more. We need to pass it's memory location and that is why we use the & sign. If you cout << &nTest, it will give you a number other than 10, this number is the nTest's memory location (effectively a pointer to an int).
This memory location is passed into ExampleFunction(int* pnInput), which does not use the original memory location number, but makes a copy of it. This copy is still the same value as &nTest. Thus when we edit the data which is held in the copy of the memory location we are actually editing the nTest variable. The copy of the memory location is forgotten/destroyed at the end of the ExampleFunction.
cout << nTest; will now print out 20
So using the above logic, if you pass in a pointer to a class object you have created, you are just giving the function the memory address of your class object. The function may use this memory location to access the class object that is being used in your main program, thus editing any values of the class stored in the given memory location or even destroying it will actually effect the object in your main program. If you don't pass it in as a pointer variable, you are making the function copy all the data in your class object which it will then edit. Anything you edit will not effect the original object in your main program. When the function finishes, the created copy will be destroyed.
As well as being pointless, it can be slower to pass data into a function without passing it in as a pointer. This is because a pointer (on a 32 bit machine) will always be 32 bits (8 bytes) in size. This is the size of an int or a float, quite small and quick to copy and destroy. If you pass in a class, it could be a lot of data that needs to be copied, thus taking more time and more memory.
Bed time for me.....