假设我有:
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/