我该怎么办& this?

时间:2012-12-06 08:51:50

标签: c++ reference this keyword semantics

我有一些代码需要使用双指针。具体来说,我很好奇为什么我不能在...... {/ p>的背景下说&this

class Obj {
public:
    void bar();
};

void foo(Obj **foopa)
{
    // do etc with your foopa.  maybe lose the foopa altogether. its nasty imo.
}

void Obj::bar()
{
    // call foo(Obj **)
    foo(&this);  // Compiler Err:  Address extension must be an lvalue or a function designator.
}

左值?功能指示器?喜欢收听。

1 个答案:

答案 0 :(得分:1)

因为"这个"是一个特殊的指针,你不应该改变它,但你可以做一些事情,不要放弃" Obj * t"在函数中,因为它在函数结束时被破坏,所以它必须是静态的。

class Obj;
Obj *t;

class Obj {
public:
    void bar();
};

void foo(Obj **foopa)
{
    // do etc with your foopa.  maybe lose the foopa altogether. its nasty imo.
}

void Obj::bar()
{
    // call foo(Obj **)
    t = this;
    foo(&t);  // Compiler Err:  Address extension must be an lvalue or a function designator.
}
相关问题