home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1927 < prev    next >
Encoding:
Internet Message Format  |  1992-12-27  |  1.8 KB

  1. Path: sparky!uunet!utcsri!skule.ecf!torn!spool.mu.edu!uwm.edu!linac!att!att!allegra!alice!bs
  2. From: bs@alice.att.com (Bjarne Stroustrup)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Must derived class reserve space for an empty base class?
  5. Message-ID: <24490@alice.att.com>
  6. Date: 27 Dec 92 14:39:07 GMT
  7. Article-I.D.: alice.24490
  8. References: <1992Dec16.202800.3398@microsoft.com> <5450@holden.lulea.trab.se> <1992Dec24.202339.26224@microsoft.com>
  9. Organization: AT&T Bell Laboratories, Murray Hill NJ
  10. Lines: 44
  11.  
  12.  
  13.  
  14. jimad@microsoft.com (Jim Adcock @ Microsoft Corporation) writes
  15.  
  16.  > Below find an example that frequently catches programmers off guard.
  17.  > Again, C++ pointers DO NOT model any notion of object identity.  If you
  18.  > want your objects to maintain a notion of object identity [A good
  19.  > idea, IMHO] then you must explicitly GIVE your objects a notion of object
  20.  > identity.  C++ comes with no such notion built-in.
  21.  > 
  22.  > #include <iostream.h>
  23.  > 
  24.  > class A { char bsPadding; };
  25.  > class B : public A {};
  26.  > class C : public A {};
  27.  > class D : public B, public C {};
  28.  > 
  29.  > main()
  30.  > {
  31.  >     D d;
  32.  > 
  33.  >     B* pb = &d;
  34.  >     C* pc = &d;
  35.  >     A* pa1 = pb; 
  36.  >     A* pa2 = pc; 
  37.  > 
  38.  >     if (pa1 == pa2)
  39.  >         cout << "pointers match\n";
  40.  >     else 
  41.  >         cout << "pointers don't match -- even though of the same"
  42.  >             " type and 'to the same object'!\n";
  43.  > 
  44.  >     return 0;
  45.  > };
  46.  
  47. Huh? pa1 and pa2 does not point to the same object of type A. They point to
  48. different sub-objects the D each of type A. 
  49.  
  50. I conjecture that people who think that pa1 and pa2 should point to the
  51. same object aren't confused about `object identity' but about the distinction
  52. between virtual and ordinary base classes (and the reason for that is often
  53. that they haven't read a good C++ textbook, but thought they could deduce
  54. the meaning of the example from first principles or experience with some
  55. other language).
  56.