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

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.

考虑一下c#的out关键字。编译器要求方法的调用者将out关键字应用于任何out参数,即使它已经知道它们是否存在。这是为了提高可读性。尽管在现代ide中,我倾向于认为这是语法(或语义)突出显示的工作。

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

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.

指针

A pointer is a variable that holds a memory address. A pointer declaration consists of a base type, an *, and the variable name. A pointer can point to any number of variables in lifetime A pointer that does not currently point to a valid memory location is given the value null (Which is zero) BaseType* ptrBaseType; BaseType objBaseType; ptrBaseType = &objBaseType; The & is a unary operator that returns the memory address of its operand. Dereferencing operator (*) is used to access the value stored in the variable which pointer points to. int nVar = 7; int* ptrVar = &nVar; int nVar2 = *ptrVar;

参考

A reference (&) is like an alias to an existing variable. A reference (&) is like a constant pointer that is automatically dereferenced. It is usually used for function argument lists and function return values. A reference must be initialized when it is created. Once a reference is initialized to an object, it cannot be changed to refer to another object. You cannot have NULL references. A const reference can refer to a const int. It is done with a temporary variable with value of the const int i = 3; //integer declaration int * pi = &i; //pi points to the integer i int& ri = i; //ri is refers to integer i – creation of reference and initialization