I've been reading the C++ FAQ Lite, and have come across a concept that I think makes sense, but I'm not sure I'm understanding correctly. It's passing by reference, value, and pointer. What I'd like is to explain what I think each means, and then if I'm wrong, perhaps get an explanation why?



Pointer

Passing by pointer is where you pass a function a pointer to the object you want worked with. You are not actually changing the object itself, just the local or temporary copy that you have made with the pointer.

This code is wrong. Both output 10 and I can not for the life of me figure out why that is. Perhaps I am using pointers wrong.

Code:
#include <iostream>

int foo(int *bar)
{
	*bar = (*bar + *bar);
	
	return(*bar);
}

int main()
{
	int baz = 5;
	int *pbaz = &baz;

	std::cout << foo(pbaz); // Should equal 10
	std::cout << "\n" << baz; // Should equal 5

	std::cin.get();
	return(0);
}


Reference

Passing by reference means that you don't pass the value of an object, you are passing the object itself. There are no copies made, only the original object is what is worked with.

Code:
#include <iostream>

int foo(int &bar)
{
	bar = (bar + bar);
	
	return(bar);
}

int main()
{
	int baz = 5;

	std::cout << foo(baz); // Should equal 10
	std::cout << "\n" << baz; // Should equal 10

	std::cin.get();
	return(0);
}


Value

Passing by value means that you are taking a copy of what has been passed to the function, work with it in the function, but do not modify the original value.

Code:
#include <iostream>

int foo(int bar)
{
	bar = (bar + bar);
	
	return(bar);
}

int main()
{
	int baz = 5;

	std::cout << foo(baz); // Should equal 10
	std::cout << "\n" << baz; // Should equal 5

	std::cin.get();
	return(0);
}


Help would be much appreciated with the passing by pointers.