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.
what's the difference between returning refrence or pointer
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.
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.
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.
Quote:Original post by InvalidPointerA "reference to a NULL pointer" would be: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.
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. ]
Quote:Original post by iMalcQuote:Original post by InvalidPointerA "reference to a NULL pointer" would be: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.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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement