c++ - 通过引用符号传递

标签 c++ pass-by-reference

我的问题非常直截了当,我相信可以理解。 当我通过引用传递值时,我做了这个简单的片段来说明我的冲突。

int main() {
 int a = 1;
 int &b = a;
}

我知道这是正确的做法,但是获取 b 的地址并使其等于 a 的值有何意义。逻辑上应该是:int &b = &a;

最佳答案

许多运算符是上下文相关的。 &“运算符”根据上下文可能表示三种不同的含义:

  1. 可以在定义引用时使用

    int& r = a;  // The variable r is a reference of the variable a
    
  2. 它可以用来获取指向某物的指针

    int* p = &a;  // Here & is the address-of operator which returns a pointer
                  // Here it makes p point to the variable a
    
  3. 可能是 bitwise AND运算符

    0x53 & 0x0f    // Here's a bitwise AND operation, the result is 0x03
    

关于c++ - 通过引用符号传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54986132/

相关文章:

c++ - 在 Nodejs 插件上使用什么。 Node.h 或 Napi.h

PHP:如何修复此类型提示错误?

c# - 将列表传递给方法而不修改原始列表

c - 从 main 传递到函数时显示不同的大小

c++ - 对 C++ 基类布局感到困惑

c++ - 可以在堆栈上声明一组指向 std::vector<int> 的智能指针吗?

c++ - set::find 和 std::find 都无法找到集合中的某些 double 元素

c++ - Qt-creator 使用 dll

php - 在 isset() 和 empty() 函数中通过引用传递

ios - 快速在 View Controller 之间通过引用传递数组