473,467 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1961
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.