TITLE: comparing address (Newsgroups: comp.lang.c++.moderated) [ JAMES KANZE ??? This is part of a discussion on object layout, the effects of inheritance on that layout, and how casting works. -adc ] My understanding of the language is that the following layout would be equally valid: Z pointer -> [Z fields] Y pointer -> [Y fields] [Y fields] [X fields] [X fields] ARMSTRONG: jonathan.k.armstrong@cdev.com (J. Karl Armstrong) Note: I have already learned I was wrong about the language standard, so It probably doesn't matter, but I will say why I _thought_ this was not the case. Your layout would, of course, imply that the values of the two pointers are not equal, but a test of equality implies they are. A simple upcast of the virtual pointer makes the code work in this case, and the compiler should do it automatically (DWP 5.9-2). CLAMAGE: clamage@Eng.Sun.COM (Steve Clamage), 5 May 96 Yes. Programmers sometimes use the term "equality" of pointers somewhat sloppily. Often there are two different concepts: Do the pointers refer to the same address in memory (which is not really a C++ source-language concept), and do the pointers compare equal. In order to compare pointers for equality, they must have the same type. In the example above, you couldn't compare a Z* to a Y* without an explicit or implicit cast of at least one of the pointers. After the necessary type conversion, the pointers would compare equal if they originally pointed to (or within) the same object, and not otherwise. That is, when converting a Base* to a Derived* or vice-versa, the compiler is responsible for adjusting the bits of the pointer so it refers to the right place. ARMSTRONG: A real dangerous case is when objects are passed through void*. CLAMAGE: Yes. Both C and C++ guarantee that you can cast any pointer to void* and back again to the original type. If t has type T*, the result of (T*)(void*)t must compare equal to the original t. (Sorry, not ANY pointer, but any data pointer. Function pointers and pointers to members are not included.) Neither C nor C++ makes any guarantees about casting the void* to any type other than back to the previous type. In C, you are unlikely to run into problems. In C++ you are very likely to run into problems if inheritance, particularly multiple inheritance, is involved. Casting to void* discards type information that may be needed to perform a cast correctly. For example, if p points to a Derived, the sequence (Base*)(void*)p might not be valid, even though (Base*)p is always valid.