C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Designated-This Parameter

From: Robin Savonen Söderholm <robinsavonensoderholm_at_[hidden]>
Date: Tue, 26 Nov 2024 15:17:33 +0100
I will not say anything about whether I think this is usable or not, but it
could be worthwhile looking at the 'this' usage in member functions in c++
23 for syntax inspiration.

// Robin

On Tue, Nov 26, 2024, 14:43 Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> We start off with:
>
> struct MyClass {
> unsigned sum = 0u, width = 0u, height = 0u;
> void SomeMethod(void);
> };
>
> And we implement the method as follows:
>
> extern void SomeGlobalFunction( MyClass* );
>
> void MyClass::SomeMethod(void)
> {
> SomeGlobalFunction( this );
> }
>
> Next we implement "SomeGlobalFunction":
>
> void SomeGlobalFunction( MyClass *const p )
> {
> p->sum = p->width + p->height;
> }
>
> But what if, instead of calling the parameter 'p', we call it 'this'.
> So now the first parameter is the "Designated-This Parameter", so that
> we can do:
>
> void SomeGlobalFunction( MyClass *const this )
> {
> this->sum = this->width + this->height;
> }
>
> which we could simplify to:
>
> void SomeGlobalFunction( MyClass *const this )
> {
> sum = width + height;
> }
>
> This would mean that we can also cast our own standalong function to a
> member function pointer:
>
> void (MyClass::*mfp)(void) = &SomeGlobalFunction;
>
> With this change to the language, we could write our own standalone
> functions outside of the class header file, i.e. the class definition
> doesn't need to change, but we can now pass our own functions (in the
> form of member function pointers) to the class and also to 3rd party
> code that uses the class.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-11-26 14:17:48