A VERY DUMB Question

Dec 17, 2011 at 3:19am
what is the difference between

void FunctionX( int* X1)

and

void FunctionY (int& X1)

for calling x1....
Dec 17, 2011 at 3:24am
the pointer will require you to put in a pointer
the adress will require you to put in an adress

you normally use the adress so that the function can use the variable the pointer is adressing
Dec 17, 2011 at 6:32am
Difference is only in calling the function and accessing the arguments

In function FunctionX,
1
2
3
4
5
6
7
8
9
void FunctionX (int* X1)
{
    cout << *X1;     // You do this to use X1.
}

// You call the function in this way
int X=50;
FunctionX(&X);


But,
In function FunctionY,
1
2
3
4
5
6
7
8
9
void FunctionY (int& X1)
{
    cout << X1;     // You do this to use X1. Note the absence of *
}

// You call the function in this way
int X=50;
FunctionY(X);    //And here too, & is missing


The former function make use of POINTERS while the later makes use of REFERENCES. References are easy to use but some prefer pointers. But internally, they both are implemented in the same way, only syntax is different
Dec 17, 2011 at 7:42am
just to add something to this. The first one is how you do pass by reference in pure C and works in both C and C++. While the second one is C++ only.
Last edited on Dec 17, 2011 at 7:42am
Dec 17, 2011 at 10:11am
Pointers can be null. References can't so if you want to be able to pass a null pointer you should pass a pointer. Otherwise I think references is to prefer in most cases
Dec 17, 2011 at 11:40am
closed account (zb0S216C)
Pointer variation:

-Requires the address of the variable to be explicitly passed to it with the unary address-of operator.
-Requires de-referencing before reading/writing to the pointed-to variable.
-Usually 4 bytes in length.

Reference Variation:

-Doesn't require the address to be explicitly passed to it.
-Doesn't require de-referencing.
-Always points to an object, be it short-term or long-term.
-The size of a reference matches the size of the type it refers to.

Wazzak
Last edited on Dec 17, 2011 at 11:41am
Dec 21, 2011 at 7:25pm
Interesting question.
Referring Framework answer:
-The size of a reference matches the size of the type it refers to

Does this mean that if the type is, say, a vector of n instances of a complex structure m bytes each, then the size of this references is n times m?
I wouldn't say that.

Regards
Dec 21, 2011 at 7:50pm
Does this mean that if the type is, say, a vector of n instances of a complex structure m bytes each, then the size of this references is n times m?

The sizeof operator doesn't take that into account. Doing sizeof(empty vector) will have the same result as sizeof(non-empty vector).
Dec 21, 2011 at 7:52pm
Another thing to note is that both allow the function to change the value of the variable passed to the function. If it will not be changed intentionally, it should be declared const.
Dec 21, 2011 at 8:18pm
Passing by reference must surely be smaller than passing a large object by value, else why bother with passing by const reference which is a very common thing.

I suspect the amount of data that needs to be copied when passing by reference is the same size as a pointer - i.e. its really a pointer under the hood.

The main advantage of references is syntax:

If you pass by reference you can use the . notation rather than the more cumbersome ->. This also means you can change to pass by value, if you need to, without having to go in and alter the code.

references also allow other things such defining the assignment operator
foo& foo::operator=(const foo&)
,
allowing functions to appear on the left hand side of =
element& container::operator[](int i)

and also method chaining such as the named parameter idiom etc.
Topic archived. No new replies allowed.