Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to return a pointer, scalar and reference in C++?

I'm moving from Java to C++ and am a bit confused of the language's flexibility. One point is that there are three ways to store objects: A pointer, a reference and a scalar (storing the object itself if I understand it correctly).

I tend to use references where possible, because that is as close to Java as possible. In some cases, e.g. getters for derived attributes, this is not possible:

MyType &MyClass::getSomeAttribute() {     MyType t;     return t; } 

This does not compile, because t exists only within the scope of getSomeAttribute() and if I return a reference to it, it would point nowhere before the client can use it.

Therefore I'm left with two options:

  1. Return a pointer
  2. Return a scalar

Returning a pointer would look like this:

MyType *MyClass::getSomeAttribute() {     MyType *t = new MyType;     return t; } 

This'd work, but the client would have to check this pointer for NULL in order to be really sure, something that's not necessary with references. Another problem is that the caller would have to make sure that t is deallocated, I'd rather not deal with that if I can avoid it.

The alternative would be to return the object itself (scalar):

MyType MyClass::getSomeAttribute() {     MyType t;     return t; } 

That's pretty straightforward and just what I want in this case: It feels like a reference and it can't be null. If the object is out of scope in the client's code, it is deleted. Pretty handy. However, I rarely see anyone doing that, is there a reason for that? Is there some kind of performance problem if I return a scalar instead of a pointer or reference?

What is the most common/elegant approach to handle this problem?

like image 557
theone Avatar asked Aug 07 '10 18:08

theone


People also ask

When should I use references and when should I use pointers?

Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don't need “reseating”. This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

Is it better to return value from a function or pass in a pointer in C?

You need to pass a pointer to a function when you need to modify the elements of an array. In C, you can't pass an array or return an array from a function. Answer for your second question is, in C function argument are passed by value.

Which is faster pointer or reference?

It's much faster and memory-efficient to copy a pointer than to copy many of the things a pointer is likely to point to. A reference is stored in as many bytes as required to hold an address on the computer. This often makes reference much smaller than the things they refer to.

Why do we need reference variables If we have pointers?

A reference variable provides a new name to an existing variable. It is dereferenced implicitly and does not need the dereferencing operator * to retrieve the value referenced. On the other hand, a pointer variable stores an address. You can change the address value stored in a pointer.


1 Answers

Return by value. The compiler can optimize away the copy, so the end result is what you want. An object is created, and returned to the caller.

I think the reason why you rarely see people do this is because you're looking at the wrong C++ code. ;) Most people coming from Java feel uncomfortable doing something like this, so they call new all over the place. And then they get memory leaks all over the place, have to check for NULL and all the other problems that can cause. :)

It might also be worth pointing out that C++ references have very little in common with Java references. A reference in Java is much more similar to a pointer (it can be reseated, or set to NULL). In fact the only real differences are that a pointer can point to a garbage value as well (if it is uninitialized, or it points to an object that has gone out of scope), and that you can do pointer arithmetics on a pointer into an array. A C++ references is an alias for an object. A Java reference doesn't behave like that.

like image 121
jalf Avatar answered Oct 16 '22 19:10

jalf