change object class

Christian Köstlin christian.koestlin at gmail.com
Sat Sep 23 06:58:43 UTC 2023


On 23.09.23 05:11, Vitaliy Fadeev wrote:
> On Friday, 22 September 2023 at 19:50:17 UTC, Christian Köstlin wrote:
>> another option could be to model your own VTable in a struct like this:
>> https://run.dlang.io/is/3LTjP5
>>
>> Kind regards,
>> Christian
> 
> Thank, Christian !
> True nice tasty solution with ```VTable```!
> And further... the project is growing.
> ```
> void Draw()
> {
>    DrawBG();
>    DrawFG();
> }
> ```
> 
> And we want use ```DrawBG()``` code  from ```initial``` in other states, 
> like ```Selected```.
> 
> How to use some functions from ```initial``` via ```VTable``` ?
> 
> I see solution in ```classes``` and methods with ```override``` keyword.
> 
> ```VTable``` does the same thing as ```__vptr``` ?

VTable is your structure .. it does exactly what you want it to do.
__vptr is the internal implementation of virtual methods in the dlang 
object model.
Line 20 and 21 in my example initialize the two `VTable`s Initial and 
Hovered. You can change VTable to contain two function pointers and 
initialize those as you like for the instances of the VTable structs.

e.g.
```d
struct DrawVTable
{
    void function(Chip, Renderer) background;
    void function(Chip, Renderer) foreground;
}

// define functions to draw the different fore and backgrounds
...
...

VTable initial = VTable(&drawInitialBackground, &drawInitialForeground);
VTable hovered = VTable(&drawHoveredBackground, &drawHoveredForeground);
VTable selected = VTable(&drawInitialBackground, &drawHoveredForegtround);

```

Kind regards,
Christian




More information about the Digitalmars-d-learn mailing list