在给函数使用原始变量时,更好的做法是什么:

unsigned long x = 4;

void func1(unsigned long& val) {
     val = 5;            
}
func1(x);

or:

void func2(unsigned long* val) {
     *val = 5;
}
func2(&x);

勒夫:有什么理由选择一个而不是另一个吗?


当前回答

这最终是主观的。到目前为止的讨论是有用的,但我不认为有一个正确或决定性的答案。这在很大程度上取决于风格指南和您当时的需求。

While there are some different capabilities (whether or not something can be NULL) with a pointer, the largest practical difference for an output parameter is purely syntax. Google's C++ Style Guide (https://google.github.io/styleguide/cppguide.html#Reference_Arguments), for example, mandates only pointers for output parameters, and allows only references that are const. The reasoning is one of readability: something with value syntax should not have pointer semantic meaning. I'm not suggesting that this is necessarily right or wrong, but I think the point here is that it's a matter of style, not of correctness.

其他回答

我真的认为你将受益于建立以下函数调用编码指南:

As in all other places, always be const-correct. Note: This means, among other things, that only out-values (see item 3) and values passed by value (see item 4) can lack the const specifier. Only pass a value by pointer if the value 0/NULL is a valid input in the current context. Rationale 1: As a caller, you see that whatever you pass in must be in a usable state. Rationale 2: As called, you know that whatever comes in is in a usable state. Hence, no NULL-check or error handling needs to be done for that value. Rationale 3: Rationales 1 and 2 will be compiler enforced. Always catch errors at compile time if you can. If a function argument is an out-value, then pass it by reference. Rationale: We don't want to break item 2... Choose "pass by value" over "pass by const reference" only if the value is a POD (Plain old Datastructure) or small enough (memory-wise) or in other ways cheap enough (time-wise) to copy. Rationale: Avoid unnecessary copies. Note: small enough and cheap enough are not absolute measurables.

这最终是主观的。到目前为止的讨论是有用的,但我不认为有一个正确或决定性的答案。这在很大程度上取决于风格指南和您当时的需求。

While there are some different capabilities (whether or not something can be NULL) with a pointer, the largest practical difference for an output parameter is purely syntax. Google's C++ Style Guide (https://google.github.io/styleguide/cppguide.html#Reference_Arguments), for example, mandates only pointers for output parameters, and allows only references that are const. The reasoning is one of readability: something with value syntax should not have pointer semantic meaning. I'm not suggesting that this is necessarily right or wrong, but I think the point here is that it's a matter of style, not of correctness.

通过const引用传递,除非有原因你希望改变/保留你传递的内容。

在大多数情况下,这将是最有效的方法。

确保对不想更改的每个形参都使用const,因为这不仅可以防止您在函数中做一些愚蠢的事情,还可以很好地向其他用户指示函数对传入的值做了什么。这包括当你只想改变所指向的对象时,创建一个const指针…

如果你有一个参数,你可能需要指出没有一个值,通常的做法是使参数为指针值并传递NULL。

在大多数情况下(从安全角度来看),更好的解决方案是使用boost::可选。这允许您通过引用传递可选值,也可以作为返回值。

// Sample method using optional as input parameter
void PrintOptional(const boost::optional<std::string>& optional_str)
{
    if (optional_str)
    {
       cout << *optional_str << std::endl;
    }
    else
    {
       cout << "(no string)" << std::endl;
    }
}

// Sample method using optional as return value
boost::optional<int> ReturnOptional(bool return_nothing)
{
    if (return_nothing)
    {
       return boost::optional<int>();
    }

    return boost::optional<int>(42);
}

指针:

可以被分配为nullptr(或NULL)。 在调用站点,如果你的类型本身不是指针,你必须使用&, 显式地修改对象。 指针可以被反弹。

引用:

不能为空。 一旦被束缚,就无法改变。 调用者不需要显式地使用&。有时这被认为是 不好,因为你必须去函数的实现看看 完成参数的修改。