Sign in to follow this  

what's the difference between returning refrence or pointer

This topic is 2666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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 valid
int& 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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
Also, a reference can't be null. A pointer can be null. That is,

int* Get() { return NULL; }; // is valid
int& 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 valid
int& 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. ]

Share this post


Link to post
Share on other sites
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 valid
int& 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.

Share this post


Link to post
Share on other sites

This topic is 2666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this