pointer equality and inheritance

S

sb

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); // - ?

will be always true?
 
P

Pete Vidler

sb wrote:
[snip]
Is there a guarantee in the standard that

Derived d;
Base* p = &d;
d.myself((void*)p); // - ?

will be always true?

I'm almost certain that there isn't. I've been bitten by this before.

-- Pete
 
L

Leor Zolman

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); // - ?

will be always true?

Adding a bit to your example, if you try this the assertion will fail.
Granted this isn't the same exact thing you posted, but I think it might be
helpful to know that multiple inheritance being involved will trigger the
assertion failure.
-leor

#include <iostream>
#include <cassert>
using namespace std;

class Base {
//...
};


class Base2 {};

class Derived : public Base2, public Base {
//...
public:
bool myself(const void* p) { return p == this;}
};

int main()
{
Derived d;
Base* p = &d;
assert(d.myself((void*)p)); // - ?

return 0;
}
 
J

John Harrison

sb said:
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); // - ?

will be always true?

No.

class Base
{
};

class Derived : public Base
{
public:
virtual ~Derived() {}
bool myself(const void* p) { return p == this; }
};

int main()
{
Derived d;
Base* p = &d;
cout << (d.myself(p) ? "true\n" : "false\n");
}

prints false on my system. The virtual destructor in Derived but not in Base
is what spoils things.

john
 
N

Nick Hounsome

sb said:
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]));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top