TheLividePianoist wrote:Thanks more but can someone explain to me the reason for the pointers and or other stuff you added. What exactly does it do?
I'm going to assume this question is about the parameter change to reference or pointer.
So basically, anytime you pass a variable as a parameter, your not actually passing that variable. Your just passing a copy of that variable.
So here's a quick example to demonstrate:
Code: Select all
void function(int x)
{
x = 3;
std::cout << x << std::endl;
}
int main()
{
int x = 2;
std::cout << x << std::endl;
function(x);
std::cout << x << std::endl;
return 0;
}
So at first glance, you'd probably expect the output to look something like:
Which makes sense considering we assigned the value of 3 to x in function(int x). Too bad we didn't actually pass x, we passed 2. So the output should actually be:
After function ends, the variable x that was set to 3 is removed from the stack. This whole idea is commonly known as passing by value.
So let's say you wanted to actually change the value of x, you can solve this by passing a pointer or reference to the object, rather than the object itself.
This means, a new object won't be created just to pass a parameter (which will probably give some undesired results in many cases anyways).
So it's more efficient to use references/pointers when passing objects, not too sure about primitive types (int, char, bool, float, etc).
Hope that helps.