iiian
iiian

Reputation: 413

What am I supposed to do with &this?

I have some code where I need to use double pointers. Specifically I'm curious as to why I can't say &this in the context where...

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.
}

lvalue? function designator? Love to hear back.

Upvotes: 0

Views: 1170

Answers (1)

Because "this" is a special pointer and you're not supposed to change it, but you can do something like, don't put "Obj *t" in the function because it's destroy at the end of function, so it's must be static.

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.
}

Upvotes: 1

Related Questions