References

Started by
0 comments, last by vbisme 23 years, 9 months ago
Take a look: void function1(int & param); //declared to take a reference void function2(int & param); //same int main() { int x = 1; function1(x); //passing by reference ( the address is passed) return (0); } void function1(int & param) { function2(param) //is this also passing by ref ( which is the address of "param" which is also the address of "x"?) } void function2(int & param) { }
Advertisement
...maybe. It doesn''t matter, really. Compilers implement references as addresses, but you should look at references as being the passed objects.

The thing to remember is that with any other type of parameter, the parameter is copied into the function. If it takes an int, the int is copied and sent to the function. If it takes a pointer, the pointer itself is copied and sent to the function (though both the copy and the original point to the same object). However, if it''s a reference, the object itself is passed to the function.

This may be implemented using pointers in your compiler, but it''s something with which you shouldn''t confuse yourself. A reference will pass the object, not a copy of the object.

This topic is closed to new replies.

Advertisement