c++ - 在可移植 C++ 构造的引用上使用 "operator &"吗?

标签 c++ pointers reference

假设我有:

void function1( Type* object ); //whatever implementation
void function2( Type& object )
{
    function1( &object );
}

假设 Type 没有重载的 operator &() 将此构造 - 在引用上使用 operator & - 获得实际所有符合标准的 C++ 编译器上的对象地址(Type 类型的变量)?

最佳答案

是的,原因是在评估任何表达式的最开始,引用将被引用的对象替换,如标准中的 5[expr]/6 所定义。这将使 &-operator 看不到任何区别:

If an expression initially has the type "reference to T" (8.3.2, 8.5.3), the type is adjusted to "T" prior to any further analysis, the expression designates the object or function denoted by the reference, and the expression is an lvalue.

这使得对表达式进行操作的任何运算符都可以“看穿”引用。

关于c++ - 在可移植 C++ 构造的引用上使用 "operator &"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1979989/

相关文章:

c++ - 使用 accumulate 计算数组 double[] 平均值的函数

pointers - Go:是否可以返回指向函数的指针

c - 打印指针指向的地址,地址指向的值,以及指针本身的地址

c - Swift/C 互操作,Swift 中的结构数据更改未在 C 中更新

rust - 为什么不能在同一结构中存储值和对该值的引用?

c++ - 在带有初始化的声明中,我可以使用对未初始化数据的引用吗?

c++ - CMake错误: install EXPORT includes target which requires target that is not in any export set

c++ - 相机旋转 (OpenGL)

c++ - 有没有办法在多映射内联中查找键的数量?

ios - 查找对 System.IO.FileSystem.Watcher.dll 的引用