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

unsigned long x = 4;

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

or:

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

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


当前回答

引用是隐式指针。基本上,你可以改变引用指向的值,但你不能改变引用指向其他东西。因此,我的观点是,如果你只想改变一个参数的值,将其作为引用传递,但如果你需要改变参数以指向一个不同的对象,则使用指针传递它。

其他回答

指针

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

引用类似于指针,除了不需要使用前缀*来访问引用所引用的值。同样,在初始化对象之后,不能引用另一个对象。

引用对于指定函数参数特别有用。

有关更多信息,请参阅“Bjarne Stroustrup”(2014)的“c++之旅”(A Tour of c++)第11-12页

引用是隐式指针。基本上,你可以改变引用指向的值,但你不能改变引用指向其他东西。因此,我的观点是,如果你只想改变一个参数的值,将其作为引用传递,但如果你需要改变参数以指向一个不同的对象,则使用指针传递它。

如果你有一个参数,你可能需要指出没有一个值,通常的做法是使参数为指针值并传递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);
}

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

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.