【问题标题】:Why C++ member function uses & in argument? [duplicate]为什么 C++ 成员函数在参数中使用 &? [复制]
【发布时间】:2012-02-03 00:36:46
【问题描述】:

可能重复:
How to pass objects to functions in C++?
Operator & and * at function prototype in class

#include <iostream>
using namespace std;

class C {
  public:
    int isSelf (C& param);
};

bool C::isSelf (C& param)
{
  if (&param == this) return true;
  else return false;
}

int main () {
  C a;
  C* b = &a;
  cout << boolalpha << b->isSelf(a) << endl;
  return 0;
}

此代码有效。但在我看来,b-&gt;isSelf(a) 真的应该是b -&gt; isSelf(&amp;a),因为isSelf 需要C 类型的地址?!

[编辑] 其他问题:

1) 有没有办法通过值传递来实现这个isSelf 函数? 2) 使用引用传递和指针传递的实现是否正确?

bool C::isSelf1(const C &c)
{
    if (&c == this) {
        return true;
    } else {
        return false;
    }
}

bool C::isSelf2(C c)
{
    if (&c == this) {
        return true;
    } else {
        return false;
    }
}

bool C::isSelf3(C * c)
{
    if (c == this) {
        return true;
    } else {
        return false;
    }
}

int main ()
{
    C c1 (2);
    C * c2 = &c1;
    cout << boolalpha;
    cout << c2 -> isSelf1(c1) << endl; // pass by reference
    cout << c2 -> isSelf2(c1) << endl; // pass by value
    cout << c2 -> isSelf3(&c1) << endl;// pass by pointer
    return 0;
}

【问题讨论】:

    标签: c++ class pointers


    【解决方案1】:

    两者之间有区别:

    bool C::isSelf (C& param)  // pass by 'reference'
                   ^^^
    

    bool C::isSelf (C* param)  // pass by 'address'
                   ^^^
    

    所以在第二版中C 对象的地址(即C*)是预期的,而不是在第一版中。

    另外请注意,内部第 1 和第 2 版本的实现可能类似;但存在语法差异。还有第三个版本:

    bool C::isSelf (C param)  // pass by value
                   ^^^
    

    对于已编辑的问题:

    1) 有没有办法使用传值实现这个isSelf() 函数?

    无法满足您的给定要求。因为它创造了一个新的价值,而这永远不会匹配。从您的代码中删除 按值传递 版本。

    除此之外,一般来说,对于任何函数,您应该选择按值传递按引用传递。你不能同时拥有两者,因为它们的函数调用语法是相同的,这会导致歧义。

    2) 使用引用传递和指针传递的实现是否正确?

    它们是正确的,但您可以将它们简化为:

    bool C::isSelf(const C &c) const // don't give postfix '1', simply overload
    {                          ^^^^^
      return (&c == this);
    }
    
    bool C::isSelf(C* const c) const // don't give postfix '3', simply overload
    {                          ^^^^^
      return (c == this);
    }
    

    另外,请参阅执行此类操作的 const 正确性 语法。

    【讨论】:

      【解决方案2】:

      问:但在我看来 b->isSelf(a) 真的应该是 b -> isSelf(&a)

      答:是的,这就是“addressof”运算符 (&) 的工作原理。 但这是一个参考参数

      在此链接中查看此示例:

      http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

      void foo(int &y) // y is now a reference
      {
          using namespace std;
          cout << "y = " << y << endl;
          y = 6;
          cout << "y = " << y << endl;
      } // y is destroyed here
      
      int main()
      {
          int x = 5;
          cout << "x = " << x << endl;
          foo(x);
          cout << "x = " << x << endl;
          return 0;
      }
      

      PS:

      这是上面的示例输出:

      x = 5
      y = 5
      y = 6
      x = 6
      

      【讨论】:

      • 有没有办法使用传值实现这个isSelf函数
      【解决方案3】:

      在函数声明中,参数名称前的 & 符号表示通过引用传递。在 C 中,这只能通过指针来完成。但是,通过引用传递,调用者不需要知道参数是通过引用传递的,而是照常使用对象。

      &amp; 字符用于地址和引用参数声明只是一个不幸的巧合。请记住,地址传递声明如下:int isSelf(C *other);

      【讨论】:

        【解决方案4】:

        函数签名中的&amp; 表示该值将作为Alias 传递,也就是说,它将是原始变量的确切内存位置。

        这与传递地址有点不同,其中参数类型本身是指向您的类型的指针,而不是精确类型。

        【讨论】:

          【解决方案5】:

          在 C++ 中,具有 C&amp; param 之类的参数的函数通过 reference 而不是值来传递它。这类似于通常将指针传递给函数的方式,但它有助于防止指针操作中的一些常见错误。

          你可能想看看这个链接的开头:http://www.cplusplus.com/doc/tutorial/functions2/

          当一个变量通过引用传递时,我们不是传递一个副本 它的值,但我们以某种方式将变量本身传递给 函数和我们对局部变量所做的任何修改都会 对作为参数传递的对应变量产生影响 对函数的调用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-12-18
            • 2019-03-15
            • 2023-03-18
            • 1970-01-01
            • 1970-01-01
            • 2015-07-15
            相关资源
            最近更新 更多