When to use references vs. pointers

前端 未结 17 2105
一向
一向 2020-11-22 02:27

I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an

17条回答
  •  终归单人心
    2020-11-22 03:26

    You properly written example should look like

    void add_one(int& n) { n += 1; }
    void add_one(int* const n)
    {
      if (n)
        *n += 1;
    }
    

    That's why references are preferable if possible ...

提交回复
热议问题