home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / cplus / 1843 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  3.1 KB

  1. Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Must derived class reserve space for an empty base class?
  5. Message-ID: <5450@holden.lulea.trab.se>
  6. Date: 18 Dec 92 15:41:08 GMT
  7. References: <1992Dec16.202800.3398@microsoft.com>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 79
  10. X-Newsreader: TIN [version 1.1 + PL8]
  11.  
  12. Jim Adcock (jimad@microsoft.com) wrote:
  13. : In article <5386@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  14. : |That two non-generic pointers of the same type (possibly converted from
  15. : |pointer-to-derived) to different objects or data members should be
  16. : |guaranteed to compare unequal.
  17.  
  18. Your example (below) shows that at least we have established what we
  19. are disagreeing on.
  20.  
  21. [...]
  22. [or else] I strongly disagree,
  23. : because I would counterclaim that pointer equality in the following
  24. : example is a legal and perfectly reasonable, and efficient, implementation.
  25. : On the contrary, requiring unnecessary and unexpected padding within
  26. : structures just to meet your new requirements would be extremely 
  27. : undesirable, in my opinion.
  28.  
  29. But the cases where "unnecessary" padding would be required would
  30. have very minor impact on the majority of programs, at least if I
  31. may consider the current implementation of virtuals (vtbl).  After all,
  32. inheriting from a truly empty base class (no virtuals) is quite uncommon,
  33. don't you think?
  34.  
  35. If pointer distinctness (as defined above) allows useful semantic constructs,
  36. such as the very common idiom for operator=:
  37. "if ( &other == this ) return", then a small and rare storage overhead
  38. in examples such as the one below could easily be tolerated (IMHO).
  39.  
  40. : If you want object addresses to be
  41. : unique, then write your code so that they are unique.
  42.  
  43. You cannot always guarantee that.  Suppose you create a class template
  44. that uses pointers to an unknown class (T*) for identity tests (the
  45. template could be specified to handle only statically allocated objects,
  46. if you like).  That template cannot know that the type parameter (T)
  47. specifies a class that will have distinct pointers to all instances,
  48. _unless_ pointer distinctness is universally guaranteed.
  49.  
  50. Imagine the confusion that migh occur if you instantiated for example
  51. a Lookup<B*> template class, using class B from your example (Lookup<>
  52. is of course defined by a class/template library).
  53.  
  54. There are existing commercial class templates that rely on pointer
  55. distinctness.  Are they invalid?
  56.  
  57. [ Jims example follows, press 'n' if you don't want to see it ]
  58.  
  59. : #include    <stdio.h>
  60.  
  61. : class A {};
  62.  
  63. : class B : public A
  64. : {
  65. : public:
  66. :     A a;
  67. : };
  68.  
  69. : main()
  70. : {
  71. :     B b;
  72. :     A* p1; 
  73. :     A* p2;
  74.  
  75. :     p1 = &b;
  76. :     p2 = &(b.a);
  77.  
  78. :     if (p1 == p2)
  79. :         printf("equal %lX %lX\n", (long)p1, (long)p2);
  80. :     else
  81. :         printf("unequal %lX %LX\n", (long)p1, (long)p2);
  82.  
  83. :     return 0;
  84. : }
  85.  
  86. -- 
  87. --------------------------------------------------------------------------
  88. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  89. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  90. --------------------------------------------------------------------------
  91.