"sb" <sp**********@yahoo.com> wrote in message
news:22**************************@posting.google.c om...
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]));