473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pointer equality and inheritance

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?
Jul 22 '05 #1
4 1959
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
Jul 22 '05 #2
On 1 Apr 2004 13:28:43 -0800, sp**********@yahoo.com (sb) wrote:
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;
}

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3

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

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
Jul 22 '05 #4

"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]));
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sb | last post by:
Given this relationship, class Base { //... }; class Derived : public Base { //... bool myself(const void* p) { return p == this}
3
by: sb | last post by:
Here is a different example, closer to what I want to do. I want to have a class that models "relations" between objects. "add" adds a relation, "check" checks if the relation exists: #include...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
26
by: Meenu | last post by:
Hi, Can two different far pointers contain two different addresses but refer to the same location in memory?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.