home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1937 < prev    next >
Encoding:
Text File  |  1992-12-30  |  3.7 KB  |  100 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Must derived class reserve space for an empty base class?
  5. Message-ID: <1992Dec30.192635.7254@microsoft.com>
  6. Date: 30 Dec 92 19:26:35 GMT
  7. Organization: Microsoft Corporation
  8. References: <5450@holden.lulea.trab.se> <1992Dec24.202339.26224@microsoft.com> <24490@alice.att.com>
  9. Lines: 89
  10.  
  11. In article <24490@alice.att.com> bs@alice.att.com (Bjarne Stroustrup) writes:
  12. |jimad@microsoft.com (Jim Adcock @ Microsoft Corporation) writes
  13. |
  14. | > Below find an example that frequently catches programmers off guard.
  15. | > Again, C++ pointers DO NOT model any notion of object identity.  If you
  16. | > want your objects to maintain a notion of object identity [A good
  17. | > idea, IMHO] then you must explicitly GIVE your objects a notion of object
  18. | > identity.  C++ comes with no such notion built-in.
  19. | > 
  20. | > #include <iostream.h>
  21. | > 
  22. | > class A { char bsPadding; };
  23. | > class B : public A {};
  24. | > class C : public A {};
  25. | > class D : public B, public C {};
  26. | > 
  27. | > main()
  28. | > {
  29. | >     D d;
  30. | > 
  31. | >     B* pb = &d;
  32. | >     C* pc = &d;
  33. | >     A* pa1 = pb; 
  34. | >     A* pa2 = pc; 
  35. | > 
  36. | >     if (pa1 == pa2)
  37. | >         cout << "pointers match\n";
  38. | >     else 
  39. | >         cout << "pointers don't match -- even though of the same"
  40. | >             " type and 'to the same object'!\n";
  41. | > 
  42. | >     return 0;
  43. | > };
  44. |
  45. |Huh? pa1 and pa2 does not point to the same object of type A. They point to
  46. |different sub-objects the D each of type A. 
  47. |
  48. |I conjecture that people who think that pa1 and pa2 should point to the
  49. |same object aren't confused about `object identity' but about the distinction
  50. |between virtual and ordinary base classes (and the reason for that is often
  51. |that they haven't read a good C++ textbook, but thought they could deduce
  52. |the meaning of the example from first principles or experience with some
  53. |other language).
  54.  
  55. I will counter-conjecture that people who consider this a trivial and
  56. obvious example have not yet realized that pointers do not model object
  57. identity in C++, in which case much of the arguments being held here
  58. are moot.
  59.  
  60. Note that I said "'to the same object'" not "to the same object"
  61.  
  62. From one point of view only legal transformations from derived to base
  63. classes have occured, so object identity must be maintained -- you're still
  64. referring "'to the same object'" as you originally were referring to.
  65. From the other point of view, clearly you end up with pointers to distinct
  66. base objects, thus you cannot be referring "'to the same object'" 
  67.  
  68. In terms of object identity:
  69.  
  70. Does pb refer to the same object as &d?
  71. Yes because B is a superclass of D
  72.  
  73. Does pc refer to the same object as &d?
  74. Yes because C is a superclass of D
  75.  
  76. Does pa1 refer to the same object as pb?
  77. Yes because A is a superclass of B
  78.  
  79. Does pa2 refer to the same object as pc?
  80. Yes because A is a superclass of C
  81.  
  82. Does pa1 refer to the same object as pa2?
  83. Yes because the chain of object identity has been maintained
  84. No because in C++ the two base objects "are distinct"
  85.  
  86. Conclusions?
  87.  
  88. Pointers do not model object identity in C++.  Template classes
  89. that assume pointers model unique identity of objects will not
  90. work in general.  If the programmer of a class is to correctly model
  91. object identity, they need to do so by how they program their class.
  92. Trying to make the language maintain a one-to-one correspondence 
  93. between pointers and object identity is not possible.  Issues
  94. of "type of pointer" "type of object" "type of derivation" "empty classes" 
  95. "vbases" "vptrs" "MI" etc all become pertinent to when or when not, how and 
  96. how not pointer comparisons should work in C++.  "General" rules are doomed to
  97. failure, because in general there is not a one-to-one correspondence
  98. between pointers and object identity in C++.
  99.  
  100.