Re: How to check for assignment to self?
"Francis Glassborow" <francis.glassborow@btinternet.com> schrieb im
Newsbeitrag news:kcqdnak7hK9iwdzanZ2dnUVZ8s2mnZ2d@eclipse.net.uk...
Matthias Hofmann wrote:
No. Take a segmented memory model where every address consists of a base
and
an offset. If the compiler compares only the offsets, then pointers to
different objects may compare equal, despite them being located in
different
segments.
Then the compiler is bugged. If two poiters to the same type compare
equal they point to the same object. The real problem is that if, for
example, you do this:
if((void *) a_ptr == (void *) b_ptr)
you may get a false positive because you have stripped the vital type
information.
Remember that given:
struct X {
int a;
} x;
x and x.a have the same address and so the only thing that prevents them
from comparing equal is that they have different types. The type is an
essential part of the rules for pointer equality.
So the compiler basically proceeds as follows?
template <class T1, class T2>
bool is_equal( T1* p1, T2* p2 )
{
return ( typeid( *p1 ) != typeid( *p2 ) )
? false : ( void* ) p1 == ( void* ) p2;
}
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]