How to overload unary & outside class?
You can overload the unary &
operator inside a class as:
struct X
{
void* operator &()
{
return this;
}
};
so that it returns an address. How would you overload it outside of a class:
struct X
{
};
void* operator &(const X& x)
{
//how?
}
Taking the address of the parameter would result in infinite recursion.
In C++11, there is template< class T > T* std::addressof(T& arg)
.
std::addressof
You can also get the same function for C++03 from Boost Utility
链接地址: http://www.djcxy.com/p/73060.html上一篇: 在班级以外重载操作员
下一篇: 如何超载一元和外部类?