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

前端 未结 13 1354
南笙
南笙 2020-12-02 18:46

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?

相关标签:
13条回答
  • 2020-12-02 19:01

    Google seems to have a strong opinion on this, and I tend to agree:

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

    0 讨论(0)
  • 2020-12-02 19:02

    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.

    0 讨论(0)
  • 2020-12-02 19:03

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

    While not a technical reason, a common style guide requirement is that any parameters that may be modified should be passed by pointer. This makes it obvious at the callsite that the object may 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!
    }
    
    0 讨论(0)
  • 2020-12-02 19:03

    This isn't specifically argument passing, but it does affect argument passing.

    You can have a container/aggregate of pointers, but not of references. Although references are polymorphic, only pointers support the "update" operation which is essential to container use (especially since you can't yet initialize references en masse, not sure if C++0x aggregate initializers will change that).

    So if you have a container full of pointers, you will usually end up managing it with functions that accept pointers rather than references.

    0 讨论(0)
  • 2020-12-02 19:06

    When you need to manipulate (e.g. resize, reassign, etc.) the address being 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 references, values of pointers can be changed and manipulated.

    0 讨论(0)
  • 2020-12-02 19:07

    A function could, conceivably, be written to do something with the memory, such as reallocating it to make it larger (returning the new pointer).

    0 讨论(0)
提交回复
热议问题