Difference between "MyClass& func1(void)" and "MyClass* func2(void)" [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the proper way to return an object from a C++ function ?
Hi there,
i would like to know whats the difference between the following two functions with respect to the return types?
MyClass& func1(void)
MyClass* func2(void)
I always thought this would be the same?
Heinrich

The first one is only capable of returning a reference to a single object and may not be null. Or rather, it should not be null.
The second one may be returning the pointer to a single object, or an array of objects.
In cases where you wish to return a single object that cannot be null, #1 tends to be the preferred form. In cases where a null can be returned #2 has to be used. Some APIs don't return references at all (like QT).
This is strictly a syntactic difference however. These two types are handled exactly the same in the compiled code: a pointer to the object (or array) will be used. That is, strictly speaking, the reference notation & adds no new semantic functionality over a normal pointer.
(This perhaps just summarizes what the other people wrote)

the first one returns reference to an object (or its address). The other one returns pointer, not reference
Major difference: the second one can return NULL

Related

Except the syntax, is there any difference between "call by reference" and "call by pointer" in C++ in terms of memory? [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 9 years ago.
MyObject obj = new MyObject(k);
foo(obj);
bar(&obj);
foo(MyObject &)
bar(MyObject *)
I know we need to use the passed parameter differently in the two functions, but apart from that, in terms of memory allocation or otherwise, is there a difference between the two? I mean for the reference also the compiler would be storing the memory pointer.
From the point of view of the compiler (i.e. the generated machine code) there is no substantial difference.
I'm not sure but I wouldn't be surprised to discover that cfront used direct translation of reference arguments to pointers.
At the language level there are however many differences; for example a reference is formally always bound to an object (i.e. you don't have a "NULL reference") and also a reference cannot be rebound (while a pointer variable can be made to point to something else).
One important difference is that a pointer can be nullptr, where as C++ has no concept of a null reference.

Obtaining an object with pointer and reference [duplicate]

This question already has answers here:
Pointer vs. Reference
(12 answers)
Closed 9 years ago.
Lets assume we have an object created statically (Type mObject;) which is exposed to the programmer by using getObject() method. I wonder what are the advantages and disadvantages in getting the object in such ways?
Type* SomeClass::getObject() {
return &mObject;
}
// the programmer types
Type* obj = someClassObj.getObject();
obj->someMethod();
and this way:
Type& SomeClass::getObject() {
return mObject;
}
// the programmer types
Type& obj = someClassObj.getObject();
obj.someMethod();
In pure C++ we use pointers quite rarely. Especially raw pointers.
Most of the code deal with object instances. As C++ have references to refer to them by identity, unlike C, taking address and passing around a pointer is reserved for situations where it is a must. Like:
when you want NULL to indicate absence
when you want to switch to different objects (reassign the pointer)
when you transfer ownership
Your example fits neither category, so you use a reference. This carries the message to the user that an object will always be there. So there's most time little point to fix the obj, you can just go ahead with someClassObj.getObject().someMethod(). The first case wants a NULL check in between.
Returning a pointer raises a lot of ambiguity/questions (could it be NULL? must I delete it?), while returning a reference to this static instance introduces guarantees. Namely, that the object will always be allocated and will always return an instance (technically, that static instance may not be constructed if you access it during initialization).
Although it is implementation defined, all implementations I know of use pointers "under the hood" when passing by reference (in the worst case). So there is no added cost to using a reference unless a copy or promotion must be introduced.
In this case, you should return a reference. In most C++ circles, references are favored over pointers when they provide all the functionality you need.

Is passing by reference is a special case of passing as pointer? [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 9 years ago.
I haven't understand passing by reference in C++ completely. I already read related questions like the following ones.
What are the differences between a pointer variable and a reference variable in C++?
Are there benefits of passing by pointer over passing by reference in C++?
When to use references vs. pointers
But even though I understand that it is different to a pointer, I don't fully understand the concept of reference in C++. How does it work? I seems like reference is a special case of a pointer and might be implemented as such by the compiler. But it behaves different on programming level. Is that correct?
Is the concept of passing by reference a special case of passing a pointer to that value? Many times I have to decide whether to use reference or pointer while programming. Moreover I want to understand the underlying principle. Is only the memory address copied?
A reference is a pointer with some special restrictions. A reference must never be un-initialized. When a reference is created it must point to something. This is useful when wanting a "pointer" that will not be NULL.
Furthermore a reference cannot have the address it holds changed, therefor it is a constant pointer.
It has some nice syntactic sugar to allow it to use the dot operator, making it useful when changing from by-value code to by-reference code.
In practice using a reference in a function as a parameter or return value usually would signify "by address, cannot be NULL and must be a valid object". Using a pointer instead would usually signify "could be NULL, so NULL must be handle". By pointer also allows for pointer arithmetic and manipulation.
What you should use is really what you're most comfortable with, assuming you understand both. I myself prefer pointers.
Edit:
As pointed out in the comments here, you cannot take the address of a reference directly, as it will return the address of what the reference is referring to. This is yet another restriction that does not apply to an average pointer.
yes, it's essentially the same as a pointer but a nicer and safer version as the pointer address cant be changed or set to null. i see it mainly as a means to have nice function interfaces without requiring the user of the interface to worry about pointers - ie function remain simple yet the object is not copied, purely its address. class copy constructor is a prime example of where references are crucial.

Can we say without aliasing, pointer and reference are of the exactly the same thing [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Both performance-wise and functionality-wise?
Which means, if you want to modifiy the contents of some data pointed by some pointer during a function call, passing pointer arguments works exactly the same as passing reference arguments?
Performance-wise, references perform slightly better than pointers. That has to do with pointer adjustment needed in certain cases, for instance when multiple inheritance is in place. When the converted pointer is null, the adjustment has to be reversed (or not done at all) so that the pointer remains null. The fact that references cannot be null mean that an extra check is not needed.
Functionality-wise, pointers can be null while references can´t, and pointers can be reassigned while references can´t. Other than the basic different syntax to access them.
So basically no, they are not exactly the same thing.

Returning vs. using a reference parameter [duplicate]

This question already has answers here:
Which is more efficient: Return a value vs. Pass by reference?
(7 answers)
Closed 1 year ago.
This is really bugging me, coming from a C# background.
Sometimes, I see functions written like this:
int computeResult();
This is what I'm used to. But then I see them written like this:
void computeResult(int &result);
I find this strange. What benefits does the second method have over the first, if any? There must be something, since I see it all the time.
There are two common reasons for such non-const reference parameters:
You may need multiple "out" parameters in a function, and using reference parameter(s) allows for this.
Your object may be expensive to copy, and so you pass in a reference that will be mutated rather than returning an object that may get copied as part of the return process. Expensive-to-copy objects may include standard containers (like vector) and objects that manage heap memory where an allocation-copy-deallocate sequence would occur. Note that compilers are getting really good at optimizing away these copies when possible and so this reason has less import than it used to.
EDIT: I should clarify that even in C++ the specific example you've provided with a single builtin type reference parameter is pretty atypical. In such cases a return value is almost always preferred.