JiiPee writes:
> On 25/08/2022 13:37, Malcolm McLean wrote:
>> On Thursday, 25 August 2022 at 06:39:56 UTC+1, Bonita Montero wrote:
>>> Am 24.08.2022 um 19:00 schrieb JiiPee:
>>>
>>>> Is there any way to check if a raw pointer is pointing to a valid object?
>>> What's the practical use of that ?
>>>
>> When you don't have good communications between the part of the program
>> which is deleting an object, and a part which holds a reference to it.
>
> Yes. When the classes cannot much communicate, and then the other class
> deletes that pointer... then my class somehow needs to be sure its still
> valid.
If the "other class deletes that pointer", then the other class must
directly, or indirectly, make provisions for that. Nothing in C++ happens
automatically, by magic.
> I created a button group manager, which stores pointers pointing to existing
> buttons. But how can this manager know if the button still exists... and
> that was the issue. I was just wondering is its poissible without changing
> code around the class.
If there's something that called a "button group manager", that's
responsible for managing these "buttons", but this manager isn't the one
that's creating these buttons, then there must be some formal process by
which thus "button group manager" must become aware of these buttons,
somehow. That also doesn't happen automatically, by magic. There must be
some procedure by which these buttons get handed over to this "button group
manager".
Therefore, there must also be a procedure by which this "button group
manager" must be notified that a particular button should no longer be under
its control. So, whatever this process or procedure is, it needs to be used
before deleting this button, so that the "button group manager" does not use
a pointer to a deleted object.
The End.