"Alf P. Steinbach" <alfps@start.no> writes:
* Danny Woods:
Is it to be expected that the addresses stored in a and b are
different?
Of course.
In general they must be, since those are pointers directly to A and B
objects.
Even though they're pointing to the same underlying instance of a C?
From an object oriented perspective, I wouldn't have expected that.
Something learned!
Try C++ destructors, they're good at cleanup.
The destructor is the issue. Let me elaborate. This is in the context
of an embedded audio/video decompression/rendering system. Almost all
object allocation is done up-front in order to minimise
allocation induced fragmentation (the system has no MMU). One component
decides which audio and video renderers to use, based upon configuration
information. It then hands these instances down to an AVSync object,
after which it has nothing more to do with them. So the AVSync object
has two pointer members: the video renderer and the audio renderer. In
the special case I describe, a *single* instance is both the audio and
video renderer, implementing both interfaces.
Occasionally, it is necessary to destroy the AVSync object and create a
new one (expensive, but unavoidable). In the destructor for AVSync, I
can't simply delete the audio and video renderers if they're both
pointing to the same underlying object.
unless I can
reliably tell that a and b point to the same object, but the simple
'a == b' doesn't work here.
Don't do that.
Alternatives?
Aw shucks, hell[1], I'd describe how to do separation of concerns, and a simple
one would be to bundle those two pointers in an object that knows how to destroy
them (individually or as one). But I don't have time and not sure you'd see the
wisdom of that from just a Usenet posting. So, since they have virtual members
you can just do dynamic_cast<void*>(a) == dynamic_cast<void*>(b). :-)