在什么情况下使用“ const T *”优于“ const T&”作为输入参数?

时间:2019-10-07 01:19:38

标签: c++ function parameters

在Google c ++代码样式中,他们表示在某些情况下使用const T*优于const T&作为输入参数。

  1. 您要传递空指针
  2. 该函数保存指向输入的指针或引用。

我不明白第二个。你能给我一些例子吗?谢谢。

1 个答案:

答案 0 :(得分:1)

  

在某些情况下,使用const T *优于const T&作为输入参数

     

2。该函数保存指向输入的指针或引用。我不明白第二个。你能给我一些例子吗

示例:

int* ptr;
void foo(int& ref)
{
    ptr = &ref; // function saves a pointer or reference to the input
                // therefore using ref input is not preferable
                // according to the guideline
}