相关疑难解决方法(0)

当操作符超载时,如何可靠地获取对象的地址?

考虑以下程序:

struct ghost
{
    // ghosts like to pretend that they don't exist
    ghost* operator&() const volatile { return 0; }
};

int main()
{
    ghost clyde;
    ghost* clydes_address = &clyde; // darn; that's not clyde's address :'( 
}
Run Code Online (Sandbox Code Playgroud)

我怎么得到clyde地址?

我正在寻找一种适用于所有类型对象的解决方案.C++ 03解决方案会很好,但我也对C++ 11解决方案感兴趣.如果可能,让我们避免任何特定于实现的行为.

我知道C++ 11的std::addressof函数模板,但我不想在这里使用它:我想了解标准库实现者如何实现这个函数模板.

c++ operator-overloading memory-address c++11

167
推荐指数
5
解决办法
2万
查看次数

有什么正当理由使一元运算符超载?

好吧,我受到启发,做了一些冲击.似乎超载operator&导致了不小的痛苦.

重载它有哪些合法案例?

(不能说我曾经那样做过......)

c++ operator-overloading

68
推荐指数
5
解决办法
3819
查看次数

标签 统计

c++ ×2

operator-overloading ×2

c++11 ×1

memory-address ×1