C++ functions: ampersand vs asterisk

前端 未结 9 1879
情书的邮戳
情书的邮戳 2020-12-13 03:43

Let\'s say you have a function that modifies a variable.

Should you write it like this: void myfunc(int *a) or like this void myfunc(int &a)

9条回答
  •  借酒劲吻你
    2020-12-13 04:22

    Something to note, if you are using stl functors, it is easier if the parameter matches the container value type.

    void foo(Bar *);
    
    void frobnicate(vector vecBars)
    {
       for_each(vecBars.begin(), 
                vecBars.end(), 
                ptr_fun(&foo));
    }
    

    The above code is much harder if foo takes Bar&

提交回复
热议问题