18 Aug, 2009, JohnnyStarr wrote in the 1st comment:
Votes: 0
I'm having a hard time with understanding pointers in c++.
I know that using the & operator in C that it is the memory address of the variable:
b = &a;

In some of the documentation I've read on c++ it shows examples like:
void foo(std::list<int>& list) { …

VS.

void foo(std::list<int> list) { …

Does the '&' operator point to the memory address of the list?
18 Aug, 2009, David Haley wrote in the 2nd comment:
Votes: 0
Normally, in C and C++, everything is passed by value and nothing is passed by reference. That means that whatever you pass in is a copy of what was passed in. Pointers are a way to pass things "by reference", but the pointer is still passed by value.

The classic example of what this means is this (if you allow me some pseudo-code):

function f(by_val x) {
x++
}
function g(by_ref x) {
x++
}
a = 1
f(a)
print(a) –> 1
g(a)
print(a) –> 2


Now, in C/C++, when you use the ampersand in "b = &a", you are taking the value of the address of 'a' (as you said).

C++ adds pass by reference semantics, so that the ampersand means that arguments are passed by reference instead of by value.

If we leave pseudo-code and use actual C++ now:

#include <iostream>
#include <stdlib>
using namespace std;
void f(int x) {
x++;
}
void g(int& x) {
x++;
}
int main() {
int a = 1;
cout << a << endl; // will print 1
f(a);
cout << a << endl; // will print 1
g(a);
cout << a << endl; // will print 2
}


(Disclaimer: I haven't actually compiled that. But if it doesn't compile, only small changes should be needed.)

In other words, the variable 'a' is being implicitly passed by reference to the function g.

In C, if you wanted to edit the parameter that was passed in, you would have to pass it as a pointer, and edit not the pointer, but the thing pointed to (the variable you want modified).

Does that clear things up?
18 Aug, 2009, JohnnyStarr wrote in the 3rd comment:
Votes: 0
DavidHaley said:
In C, if you wanted to edit the parameter that was passed in, you would have to pass it as a pointer, and edit not the pointer, but the thing pointed to (the variable you want modified).


So, in C, we wouldn't be able to do this then?
void pointless(CHAR_DATA& ch) {…


But, if we were passing our CHAR_DATA by reference in c++ we would?
Or is the difference that if we passed by reference, we would use the '.' operator instead of the
'->' arrow pointer?

// in pointless function
void pointless(CHAR_DATA& ch) {
ch.levelup(); …


I'm really sorry if i missed the point! <- bad pun :redface:
18 Aug, 2009, David Haley wrote in the 4th comment:
Votes: 0
staryavsky said:
So, in C, we wouldn't be able to do this then?
void pointless(CHAR_DATA& ch) {…

Correct: that's invalid C syntax.

Quote
But, if we were passing our CHAR_DATA by reference in c++ we would?

Yes.

Quote
Or is the difference that if we passed by reference, we would use the '.' operator instead of the
'->' arrow pointer?

// in pointless function
void pointless(CHAR_DATA& ch) {
ch.levelup(); …

This is just a syntax thing really. Pointers are dereferenced using '->' whereas things on the stack – including references – are accessed using '.'.

You can, for example, have pointers to CHAR_DATA passed by reference, and so you'd still access the character by using '->'…
18 Aug, 2009, JohnnyStarr wrote in the 5th comment:
Votes: 0
So, if you can pass a variable by pointer or reference, what is the advantage of
using a pointer? If i were to pass a reference to CHAR_DATA& instead of a pointer CHAR_DATA*, what is the real difference? They are both managed on the heap right?

EDIT: I just remembered; a pointer can be reasigned and a reference can not.
Am I right?
18 Aug, 2009, David Haley wrote in the 6th comment:
Votes: 0
Well… if you pass a pointer by reference, you're passing the value of that pointer by reference, so you can change what it points to and have that change propagate back to the caller. Imagine the example I gave, but with a pointer instead of an int.

The advantage of using a pointer is that in C (not C++) it was impossible to use references. :wink:
Also, the pointer makes it very explicit that the thing pointed to is passed by reference, whereas the reference syntax makes it "magic".

Semantically, there is no difference between passing ch as a reference, or passing a pointer to ch. (At least, not that I can think of right now…) The differences are mainly things like you must pass a reference to an actual thing, whereas you can pass in NULL pointers – there is no such thing as a NULL reference. (But you can have a reference to a NULL pointer…) I'm not sure if you would count this as a semantic difference, though.

Quote
If i were to pass a reference to CHAR_DATA& instead of a pointer CHAR_DATA*, what is the real difference? They are both managed on the heap right?

They're not necessarily both on the heap. Being a pointer makes no indication as to whether or not something is on the heap. You can have a pointer to something on the stack or on the heap. A reference however is to an actual variable, and so that variable is (basically) on the stack (unless it's a global or data page variable or whatever – but never mind that for now).
19 Aug, 2009, JohnnyStarr wrote in the 7th comment:
Votes: 0
Fantastic explanation. :smirk:
0.0/7