Re: pointer equality and inheritance
From: Nick Hounsome (nh002_at_blueyonder.co.uk)
Date: 04/03/04
- Next message: Ben Measures: "Re: size and nomenclature of integral types"
- Previous message: Nick Hounsome: "Re: Newbie question : constructor return type ?"
- In reply to: sb: "pointer equality and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 3 Apr 2004 15:28:51 +0100
"sb" <spam_bait101@yahoo.com> wrote in message
news:221dd125.0404011328.4f48de15@posting.google.com...
> Given this relationship,
>
> class Base {
> //...
> };
>
>
> class Derived : public Base {
> //...
> bool myself(const void* p) { return p == this}
> };
>
>
>
> Is there a guarantee in the standard that
>
> Derived d;
> Base* p = &d;
> d.myself((void*)p); // - ?
You dont need the cast
>
> will be always true?
No for the reasons others have given but why would you compare a void
pointer anyway?
Why not do the sensible thing:
d == p;
If you don't actually have a Base* (which makes your example incorrect) then
you MUST
make sure that the void pointer always pointed to the same sort of thing
then you can cast back and compare:
Derived d; // general case derived in complex way
Base b;
Unrelated u; // If you are not storing some unrelated class
//as well then you should be using Base* not void*
void* p[3] = {static_cast<Base*>(&d), &b, &u };
assert(&d == static_cast<Base*>(p[0]));
assert(&b == static_cast<Base*>(p[1]));
assert(&d != static_cast<Base*>(p[2]));
- Next message: Ben Measures: "Re: size and nomenclature of integral types"
- Previous message: Nick Hounsome: "Re: Newbie question : constructor return type ?"
- In reply to: sb: "pointer equality and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|