Wednesday, August 24, 2022

Digest for comp.lang.c++@googlegroups.com - 6 updates in 2 topics

Malcolm McLean <malcolm.arthur.mclean@gmail.com>: Aug 24 12:45PM -0700

On Wednesday, 24 August 2022 at 18:00:43 UTC+1, JiiPee wrote:
> Assuming I cannot change the Parent class in the above example.
> Or is there any pattern applied to Child class... again assuming we
> cannot change the Parent class at all.
 
The only way to do it is to overload the new and delete operators to maintain a
table of valid pointers. You can then test the pointers for validity (though note
that memory might be re-used, so you have to have something more sophisticated
than a simple list).
This isn't practical unless you are building a system from the ground up.
Louis Krupp <lkrupp@invalid.pssw.com.invalid>: Aug 24 01:51PM -0600

On 8/24/2022 11:41 AM, JiiPee wrote:
>>    of the notifications required:
 
> Yes possible, and good if can change parent. But that would need to
> change the Parent class which I cannot always do.
 
Wild guess: Could you have a class that contains a Parent object and
calls its methods while doing notifications and anything else you need?
If you don't use pointers to the Parent class itself, you might not have
to change it.
 
Louis
JiiPee <kerrttuPoistaTama11@gmail.com>: Aug 24 11:17PM +0300

On 24/08/2022 22:45, Malcolm McLean wrote:
> The only way to do it is to overload the new and delete operators to maintain a
> table of valid pointers.
 
Yes, that is a new idea. At least can consider...
scott@slp53.sl.home (Scott Lurndal): Aug 24 08:54PM

>> The only way to do it is to overload the new and delete operators to maintain a
>> table of valid pointers.
 
>Yes, that is a new idea. At least can consider...
 
If every class derives from a common base class, the base class
can contain a magic number that identifies the object as valid.
 
e.g.
 
class Object {
static const uint64_t MAGIC_NUMBER = 0x23324abc3242ffaaul;
uint64_t o_magic;
 
public:
Object(void) : o_magic(MAGIC_NUMBER) {}
 
bool is_valid(void) const { return o_magic == MAGIC_NUMBER; }
};
 
class MyObject : Object {
...
};
 
MyObject *objp = ...;
 
if (objp->is_valid()) {
this is a valid, initialized object.
}
 
 
This approach also allows some degree of introspection if you
add additional member functions to the Object class.
 
The JAVA object system uses this concept.
Malcolm McLean <malcolm.arthur.mclean@gmail.com>: Aug 24 02:57PM -0700

On Wednesday, 24 August 2022 at 21:55:07 UTC+1, Scott Lurndal wrote:
 
> This approach also allows some degree of introspection if you
> add additional member functions to the Object class.
 
> The JAVA object system uses this concept.
 
On some systems, if you delete objp, then call is_valid() on it, you
will get a segmentation fault. The implementation efectively has its
own mechanism for checking for valid objects, and "protects" the
program by shutting it down if an invalid object is accessed.
Ben Bacarisse <ben.usenet@bsb.me.uk>: Aug 24 09:48PM +0100

>> implementation so it should not break anything.
 
> C23 will make bool, false, and true keywords.
 
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf
 
I didn't know that, but I am not surprised because of the new course C
is taking. I hope it does not run aground...
 
--
Ben.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: