C++ Logo

std-proposals

Advanced search

[std-proposals] Set 'this' in non-member function

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 9 Oct 2023 07:16:25 +0100
It would be cool if we could do the following:

class Monkey {
    int i;
};

void Func(Monkey &arg)
{
    Monkey *const this = &arg;

    i = 6; // Implicitly tries 'this->i'
}

int main(void)
{
    Monkey obj;
    Func(obj);
}

To give an example, when I'm writing a desktop GUI program in
wxWidgets, I sometimes have to write a standalone function outside the
class, and then I copy-paste a load of code from the class
implementation into my own standalone function -- but it doesn't
compile because I need to write "this->' in front of all the accesses
of member variables/functions.

Received on 2023-10-09 06:16:37