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

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

Can someone add more cases?

+33
c ++
Mar 31 '10 at 3:46
source share
13 answers

Some, such as pass-by-pointer, are better off when the object being passed is actually modified. They use pass-by-const-reference when the object is passed by reference to avoid copying the object, but will not change in the function.

As an illustration, perform 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 

When calling foo vs foo1, it is not obvious from the point of view of callers (or programmers reading the code) that the function can change testInt without looking at the signature of the function. Looking at foo2, the reader can easily see that the function can actually change the value of testInt because the function gets the address of the parameter. Note that this does not guarantee that the object is actually modified, but one that helps in using references or pointers. In general, if you want to follow this guide sequentially, you should always pass const links when you want to avoid copying, and pass a pointer when you want to change the object.

+39
Mar 31
source share

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

Use links when you can and pointers when you need to.

Links are usually preferable to pointers when you do not need "Reinstall." This usually means that links are most useful in a class. Recommendations usually appear on the skin of an object and pointers inside.

An exception to the above is that a function parameter or return value needs a "control" link - a link that does not apply to the object. This is usually best done by returning / taking a pointer and giving a NULL pointer this special significance (links should always be aliases, not NULL with dereferencing pointer).

Note. Old-line C programmers sometimes don't like links because they provide link semantics that are not explicitly in the caller's code. After however, C ++ experience quickly realizes that this is a form of hiding information that is an asset and not a liability. For example., Programmers should write code in the language of the problem, not the language of the machine.

+20
Mar 31 '10 at 6:27
source share

You have many situations in real-world programming where the parameter does not exist or is invalid, and this may depend on the semantics of the execution code. In such situations, you can use NULL (0) to signal this state. Besides,

  • A pointer can be reassigned to a new state. Link cannot. This is desirable in some situations.
  • A pointer helps transfer the owner of semantics. This is especially useful in a multi-threaded environment if the State of a parameter is used to execute in a separate thread, and you do not usually poll until you exit.

Although, if you spend enough time developing the code, these situations can be avoided; in practice, this is not possible every time.

+6
Mar 31 '10 at 4:20
source share

In addition to some other answers about property semantics (especially factory functions).

Although this is not a technical reason, the general requirement for a style guide is that any parameters that can be changed must be passed in by a pointer. This makes it obvious in callsite that the object can be modified.

 void Operate(const int &input_arg, int *output_arg) { *output_arg = input_arg + 1; } int main() { int input_arg = 5; int output_arg; Foo(input_arg, &output_arg); // Passing address, will be modified! } 
+4
Mar 31 '10 at 4:25
source share

Rule number one for this: if NULL is a valid value for a function parameter in the context of the function, pass it as a pointer, otherwise pass it as a link.

The rationale is that if it cannot (should not!) Ever be NULL, then do not put yourself at the trouble of checking for NULL or the risk of problems due to the fact that it is NULL.

+4
Mar 31 '10 at 6:35
source share

When working with raw memory (for example, when creating your own memory pool), you should use a pointer. But you're right, in plain code, the only use for a pointer is an optional parameter.

+1
Mar 31
source share

In C ++, in most cases it is not enough to skip the pointer. You should consider alternatives first: patterns, (const) links, containers, strings, and smart pointers. However, if you must support legacy code, you will need to use pointers. If your compiler is minimalistic (e.g. embedded systems), you will need pointers. If you need to talk to the C library (some kind of system library for a specific driver that you work with?), You will need to work with pointers. If you want to deal with very specific memory offsets, you will need pointers.

In C, pointers are first-class citizens; they are too fundamental to think about eliminating them.

+1
Mar 31 '10 at 3:55
source share

Every time you pass a pointer to a function. Or if you have a method of "reset" a la auto_ptr.

+1
Mar 31 '10 at 4:00
source share

When you need to manipulate (for example, resize, reassign, etc.) the address pointed to by the pointer inside the function, then you need a pointer.

For example:

 void f( int* p ) { // .. delete []p; p = new int[ size ]; //... } 

Unlike links, pointer values ​​can be modified and manipulated.

+1
Mar 31 '10 at 4:30
source share

This is not a specific argument passing, but it does affect passing arguments.

You may have a container / collection of pointers, but not links. Although links are polymorphic, only pointers support the β€œupdate” operation, which is necessary for use in the container (especially since you cannot yet initialize links in bulk, you are not sure if this will change C ++ 0x initializers).

So, if you have a container with pointers, you will usually manage it with functions that take pointers, not links.

+1
Mar 31 '10 at 5:29
source share

You can perhaps write some function to do something with memory, for example, reallocate it to increase it (returning a new pointer).

0
Mar 31 '10 at 4:00
source share

If I need to pass an array of objects, I need to follow the pointer. Arrays are not always stored in std::vector<> .

In addition, pass by pointer allows NULL and pass by reference, no, so I use this distinction as a contract, similar to NULL columns vs. NOT NULL in SQL, as well as the function return bool : true = failed and false = failed, vs. function return int : return value is the result code, where 0 = success and others fail. "

0
Mar 31 '10 at 4:05
source share

Google seems to be strongly of this opinion, and I tend to agree:

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Reference_Arguments

0
Jun 28 '13 at 6:02
source share



All Articles