Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When pass-by-pointer is preferred to pass-by-reference in C++?

Tags:

c++

I can imagine one case, in which the input parameter could be NULL so that pass-by-pointer is preferred but not pass-by-reference?

Can anybody add more cases?

like image 405
skydoor Avatar asked Mar 31 '10 03:03

skydoor


People also ask

Is it better to pass by pointer or by reference?

References are usually preferred over pointers whenever we don't need “reseating”. Overall, Use references when you can, and pointers when you have to. But if we want to write C code that compiles with both C and a C++ compiler, you'll have to restrict yourself to using pointers.

Why would we want to pass a pointer by reference?

You would want to pass a pointer by reference if you have a need to modify the pointer rather than the object that the pointer is pointing to. This is similar to why double pointers are used; using a reference to a pointer is slightly safer than using pointers.

When should you pass by reference?

If an argument is significant in size (like a string that's a list), it makes more sense to use pass by reference to avoid having to move the entire string. Effectively, pass by reference will pass just the address of the argument and not the argument itself.

Are references always passed by pointers?

The pointer content is passed by reference but the pointer itself is still passed by value, i.e. reassinging it to some other pointer will not be reflected upon the exit from the method because the pointer will be set to point to the same memory block as before the call. Think of it as a simple int variable.


3 Answers

Some like pass-by-pointer better in cases where the object being passed is actually going to be modified. They use pass-by-const-reference when the object is being passed by reference in order to avoid a copy of the object, but will not be changed in the function.

In illustration, take the following functions:

int foo(int x); int foo1(int &x); int foo2(int *x); 

Now in the code, I do the following:

int testInt = 0;  foo(testInt);   // can't modify testInt foo1(testInt);  // can modify testInt  foo2(&testInt); // can modify testInt 

In calling foo vs foo1, it's not apparent from the callers perspective (or a programmer reading the code) that the function can modify testInt without having to look at the signature of the function. Looking at foo2, a reader can easily see that the function may in fact modify the value of testInt because the function is receiving the address of the parameter. Note that this doesn't guarantee the object is actually modified, but that's where being consistent in the use of references vs. pointers helps. In general, if you want to follow this guideline consistently you should always pass const references when you want to avoid copies, and pass by pointer when you want to be able to modify the object.

like image 184
RC. Avatar answered Oct 03 '22 00:10

RC.


The C++ FAQ has a very good answer for this question:

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.

The exception to the above is where a function's parameter or return value needs a "sentinel" reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references should always alias objects, not a dereferenced NULL pointer).

Note: Old line C programmers sometimes don't like references since they provide reference semantics that isn't explicit in the caller's code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine.

like image 39
Thomas Bonini Avatar answered Oct 02 '22 23:10

Thomas Bonini


You have many situations in real world programming wherein a parameter does not exist or is invalid and this can depend on runtime semantics of the code. In such situations you can use NULL (0) to signal this state. Apart from this,

  • A pointer can be re-assigned to a new state. A reference cannot. This is desirable in some situations.
  • A pointer helps transfer owner-ship semantics. This is especially useful in multi-threaded environment if the parameter-state is used to execute in a separate thread and you do not usually poll till the thread has exited.

Although if enough time is spent designing the code properly, these situations can be avoided; in practice it is not possible everytime.

like image 23
Abhay Avatar answered Oct 02 '22 23:10

Abhay