kildom / cppUtils

Small C++ utility library inspired by high level languages like Python or JavaScript
0 stars 0 forks source link

Prevent deallocating `this` pointer #2

Open kildom opened 1 month ago

kildom commented 1 month ago

If, during executing of method, dollar reference that was originally used to call this method causes de-allocation of the object, this pointer becomes invalid.

To prevent this from happening, the -> operator may return some helper type which increments the ref counter and also have -> operator returning actual pointer to object. This way object will never be de-allocated during execution of method. Simple assignment to field will also use the same procedure, but optimizer should remove increment and decrement, but it will not be able to remove comparing ref counter to zero at the end.

The * operator becomes problematic, but maybe it should be completely removed since class designed to work with dollar ref should not need to be converted to actual instance. If there is a really good reason to do that, user can use a method returning reference to object, e.g. T& getInstance().