what's the difference between returning refrence or pointer

Started by
4 comments, last by InvalidPointer 13 years, 7 months ago
is there any difference between the following.

int& Get() { return &myInt };
int* Get() { return &myInt };

and if i return it as const does it really limit me from calling an functions on that pointer that aren't const?

Thanks for the response, i just had this idea pop in my head and I gotta run out the door so no time to test it.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Advertisement
int& Get() { return &myInt };

You're returning the address of myInt. It should be:
int& Get() { return myInt; }; //EDIT: the compiler will take care of defining the return value as a reference

Also, a reference can't be null. A pointer can be null. That is,
int* Get() { return NULL; }; // is validint& Get() { ???? }; // you can't put anything here that will return null. The compiler won't let you.

Also, references can't be reassigned.
int& a = Get();a = SomeOtherGet(); // can't be done. "a" is defined as the reference returned by Get(). Within it's scope, that's all "a" can be.

A more complete explanation here.
Quote:if i return it as const does it really limit me from calling an functions on that pointer that aren't const?

Correct. The intent for declaring something const is to let the compiler know that, as an output from a function, a change to that value is not to be allowed. As an input to a function, a change to an input reference value is not to be allowed within the function.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
Also, a reference can't be null. A pointer can be null. That is,
int* Get() { return NULL; }; // is validint& Get() { ???? }; // you can't put anything here that will return null. The compiler won't let you.


Actually, this is a common and incorrect belief-- there is nothing preventing a reference to a NULL pointer. To be slightly more pedantic, a reference will always refer to a legal instance of whatever object type the reference is of-- the difference is subtle, but important. While *dereferencing* a NULL is undefined behavior, the pointer itself, strictly speaking, is still valid.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.
Quote:there is nothing preventing a reference to a NULL pointer ... a reference will always refer to a legal instance of whatever object type the reference is of

You are correct. The difference is, indeed, subtle.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by InvalidPointer
Quote:Original post by Buckeye
Also, a reference can't be null. A pointer can be null. That is,
int* Get() { return NULL; }; // is validint& Get() { ???? }; // you can't put anything here that will return null. The compiler won't let you.


Actually, this is a common and incorrect belief-- there is nothing preventing a reference to a NULL pointer. To be slightly more pedantic, a reference will always refer to a legal instance of whatever object type the reference is of-- the difference is subtle, but important. While *dereferencing* a NULL is undefined behavior, the pointer itself, strictly speaking, is still valid.
A "reference to a NULL pointer" would be:
int *a = NULL;int *&b = a;

However that is not the situation we have here. In the example Bucyeye posted, he is perfectly correct, as what is not valid according to the lanaguage is a NULL-reference.

I refer you to Wikipedia where the C++ standard is quoted as saying:
Quote:A reference shall be initialized to refer to a valid object or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior. As described in 9.6, a reference cannot be bound directly to a bitfield. ]
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Quote:Original post by InvalidPointer
Quote:Original post by Buckeye
Also, a reference can't be null. A pointer can be null. That is,
int* Get() { return NULL; }; // is validint& Get() { ???? }; // you can't put anything here that will return null. The compiler won't let you.


Actually, this is a common and incorrect belief-- there is nothing preventing a reference to a NULL pointer. To be slightly more pedantic, a reference will always refer to a legal instance of whatever object type the reference is of-- the difference is subtle, but important. While *dereferencing* a NULL is undefined behavior, the pointer itself, strictly speaking, is still valid.
A "reference to a NULL pointer" would be:
int *a = NULL;int *&b = a;

However that is not the situation we have here. In the example Bucyeye posted, he is perfectly correct, as what is not valid according to the lanaguage is a NULL-reference.

I refer you to Wikipedia where the C++ standard is quoted as saying:
Quote:A reference shall be initialized to refer to a valid object or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior. As described in 9.6, a reference cannot be bound directly to a bitfield. ]

And I don't argue that-- my correction has to do with the belief that 'references can't be NULL' in the more literal sense :)

EDIT: Ah, I see what you're getting at here. Adjusted.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.

This topic is closed to new replies.

Advertisement