bar is a pointer, but the addresses are still different. That's because the bar from main and the bar from foo are two separate entities. At this point you have enough information to understand what's wrong in your code.

What happens is you pass a copy of the pointer to doSomething and then assign to the copy, but the original is still unchanged. The solution when you want to pass by reference is to simulate it with a pointer, even if the original object is already a pointer: