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

前端 未结 13 1416
南笙
南笙 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:07

    If I need to pass an array of objects, I need to pass by pointer. Arrays aren't always stored in std::vector<>.

    Other than that, pass by pointer allows NULL and pass by reference doesn't, so I use that distinction as a contract much like NULL vs. NOT NULL columns in SQL, and loosely like "function returning bool: true = succeeded and false = failed, vs. function returning int: return value is a result code where 0 = success and others are failures".

提交回复
热议问题