如果addressof operator&
运行良好,那么为什么C++引入了addressof()
函数?该&
运营商从一开始的C++部分-为什么推出这个新功能呢?它是否比C的&
运营商具有任何优势?
Bau*_*gen 137
operator&
对于类类型,一元可能会重载,以便为您提供除对象地址之外的其他内容,同时 std::addressof()
始终会为您提供实际地址.
举例:
#include <memory>
#include <iostream>
struct A {
A* operator &() {return nullptr;}
};
int main () {
A a;
std::cout << &a << '\n'; // Prints 0
std::cout << std::addressof(a); // Prints a's actual address
}
Run Code Online (Sandbox Code Playgroud)
如果你想知道这样做是否有用:有
什么合理的理由来重载一元运算符&?
归档时间: |
|
查看次数: |
4065 次 |
最近记录: |