【问题标题】:What does "ampersand operator" mean between a class and a variable?类和变量之间的“与号运算符”是什么意思?
【发布时间】:2018-02-22 21:58:32
【问题描述】:

我在plumed中看到了如下代码,很困惑:

void ActionAtomistic::makeWhole() {
for(unsigned j=0; j<positions.size()-1; ++j) {
    const Vector & first (positions[j]);
    Vector & second (positions[j+1]);
    second=first+pbcDistance(first,second);
  }
}

谁能告诉我这里用什么“&”?我用谷歌搜索了“类和变量之间的 c++ & 符号”,但没有找到答案。

更新:我知道什么是引用,但认为 Vector 和“&”之间不应该有任何空格。谢谢你们澄清这一点。

【问题讨论】:

  • 表示Vector&amp;的类型。它是一个引用类型。有关该主题的更多信息:reference declaration.
  • I googled "c++ ampersand between class and variable" but did not find the answer. 有趣。使用该查询的第一个谷歌搜索结果会导致答案。
  • "认为 Vector 和 "&"" 之间不应该有任何空格 - 在这种情况下空格无关紧要。 const Vector&amp; firstconst Vector &amp;firstconst Vector &amp; first,它们都是一样的。

标签: c++


【解决方案1】:

这意味着first 是对Vector 类型对象而不是Vector 类型对象的引用(在本例中为const 引用)。

阅读更多关于参考here

【讨论】:

    【解决方案2】:

    这被称为参考。我通常会写 Type&amp; name 这样的引用来明确引用是类型的一部分。

    引用就像更容易使用但有一些限制的指针。以下是您何时可以使用参考的示例:

    void add1ToThisNumber(int& num) {
        num += 1;
    }
    // elsewhere...
    int myNumber = 3;
    add1ToThisNumber(myNumber);
    cout << myNumber; // prints 4
    

    【讨论】:

    • 我同意引用是类型的一部分。但是,对于变量和成员,我更喜欢将 &amp; 粘贴到名称上(只要可能在类型之后列出多个标识符。例如:int&amp; var1, var2; 可能会让您认为两者都是引用,但这是错误的。实际上是 int &amp;var1, var2;第二种风格使这一点更清楚。(恕我直言);-)
    【解决方案3】:

    reference(在这种情况下)基本上是另一个变量的别名。虽然以下内容不适用于第一种情况(因为您的引用是 const),但引用可用于修改它们所引用的对象。举个例子:

    int c = 5;
    int& d = c;
    d = 12; // c is set to 12
    

    在您的特定情况下,引用是不可变别名,因此无法通过 first 修改 positions[j]

    在第二种情况下,执行second = variable 将评估为positions[j + 1] = variable

    【讨论】:

      【解决方案4】:

      &amp; 根据上下文有不同的含义。

      1. 声明一个类型。

        int var;
        int& ref1 = var;           // Declares a reference to a variable
        int const& ref2 = var;     // Declares a const reference to a variable
        
        int& foo();                // Declares foo() whose return type is reference to an int
        void bar(int&);            // Declares bar whose argument type is reference to an int
        
        struct Foo
        {
           int& bar;              // Declares bar to be member variable of the
                                  // class. The type is reference to an int
        };
        
      2. 获取变量的地址(实际上是任何左值)

        int var;
        int* ptr = &var;           // Initializes ptr with the address of var
        
        int arr[4];
        int* ptr2 = &(arr[3]);     // Initializes ptr2 with the address of the
                                   // last element of arr
        
      3. 执行按位与运算。

        int i = <some value>;
        int j = <some value>;
        int k = (i & j);          //  Initializes k with the result of computing
                                  //  the bitwise AND of i and j
        

      您的代码中的内容是第一次使用。

      const Vector & first (positions[j]);
      

      该行声明firstconstposition[j] 的引用。

      【讨论】:

      • 也许也值得一提int arr[4]; int(&amp;ref)[4] = arr;
      • @KillzoneKid,这肯定是一个有趣的案例,但对 OP、IMO 没有用处。如果他们不理解简单的声明,我认为添加对数组的引用作为示例就太过分了。
      猜你喜欢
      • 2015-04-05
      • 2013-05-07
      • 1970-01-01
      • 2015-09-18
      • 2017-03-29
      • 2011-03-16
      • 2011-07-09
      • 2016-07-23
      相关资源
      最近更新 更多