0

我有一些代码需要使用双指针。具体来说,我很好奇为什么我不能&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.
}

左值?功能指示符?喜欢听回音。

4

1 回答 1

1

因为“this”是一个特殊的指针,你不应该改变它,但你可以做一些事情,不要把“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.
}
于 2012-12-06T09:02:20.380 回答