C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Versatility -- Classes change at runtime

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 31 Jul 2022 10:06:59 -0400
On Sun, Jul 31, 2022 at 6:40 AM Marcin Jaczewski via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Would all this be simply solved by some syntax sugar?
> ```
>
> struct Foo
> {
> using MebPtr = int (Foo::*)(int);
>
> int A(int);
> int B(int);
>
> static MebPtr StaticPointer;
> MebPtr Pointer = &Foo::A;
> };
> Foo::MebPtr Foo::StaticPointer = &Foo::A;
>
> int main()
> {
> Foo foo;
>
> (foo.*foo.StaticPointer)(10); // work now
> (foo.*foo.Pointer)(10);
>
> foo.Pointer(10); //error now, will be same as `(foo.*foo.Pointer)(10);`
> foo.Pointer = &Foo::B;
> foo.Pointer(10);
>
> foo.StaticPointer(10); //error now, will be same as
> `(foo.*foo.StaticPointer)(10);`
> foo.StaticPointer = &Foo::B;
> foo.StaticPointer(10);
> }
> ```
> And you can make your own virtual table, that could be dynamic per
> object or per type.

It should be noted that with explicit object parameter functions, this
process is even easier since they resolve down to regular function
pointers.

Received on 2022-07-31 14:07:12