change object class

evilrat evilrat666 at gmail.com
Sun Sep 17 17:10:16 UTC 2023


On Sunday, 17 September 2023 at 15:05:59 UTC, Vitaliy Fadeev 
wrote:
> It works! But I want to ask how to make this 100% the best of 
> the best?
> What should I consider before changing ```__vptr``` ?

If that works for you with that constraint of having exact memory 
layout then it should be ok.

This however is very uncommon pattern and your library users 
might reject it so keep that in mind if you are going to make 
public library.

Other than that I would suggest at least to make that cast method 
to return a shallow copy because messing with "this" ptr can be 
dangerous (make sure to try it with const objects and optimized 
release builds before using this everywhere).

An even better (at least safer, in theory) option would be to 
make "View" or handle struct that wraps an object(pointer) and 
tracks such transformations.

Of course to think of it now there is yet another opportunity - 
why not to look for something like ECS then?
Because you seem to already treat your objects purely as data 
containers, that way you can safely detach data from logic and 
reduce the scope of your components to keep them focused on one 
task.
That is up to you of course.



More information about the Digitalmars-d-learn mailing list