disccip disccip - 1 month ago 12
C++ Question

"expression must be an l-value or function designator" error when taking the address of this

I'm trying to do this in C++:

class Abc
{
int callFunction1()
};

void function1(Abc** c1) {//do something}

int Abc::callFunction1()
{
function1(&this);
return 0;
}


And I get "expression must be an l-value or function designator" error in visual studio 2015. So I don't understand where I go wrong. To my knowledge,
&this
should have the type
Abc**
right?

The function definition isn't mine to change. So I can't just change the parameter type.

Answer Source

The expression this is an rvalue, the same way that the expressions 137 or 'a' are, and so you can't take its address.

If you want to get a pointer to a pointer to this, you'll need to create a new variable of the right type:

auto* ptr = this;
doSomething(&ptr);