home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1916 < prev    next >
Encoding:
Text File  |  1992-12-24  |  2.1 KB  |  66 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: <1992Dec24.202339.26224@microsoft.com>
  6. Date: 24 Dec 92 20:23:39 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec16.202800.3398@microsoft.com> <5450@holden.lulea.trab.se> <85744@ut-emx.uucp>
  9. Lines: 55
  10.  
  11. In article <85744@ut-emx.uucp> jamshid@ut-emx.uucp (Jamshid Afshar) writes:
  12. |The fact the following idiom works under all current C++
  13. |implementations, and that it will probably continue to work no matter
  14. |what ANSI says, is what most worries me and why I originally posted
  15. |this question.  Programmers will continue to do things like
  16. |    class Register {
  17. |       Set<Register*> alive;
  18. |    protected:
  19. |       Register() { alive += this; }
  20. |       virtual ~Register() { alive -= this; }
  21. |       static void foo();  // do something to all registered objects
  22. |    };
  23. |
  24. |If a special-case rule is not added making the above safe on *any* C++
  25. |implementation, I hope more C++ literature will point out the fact
  26. |that the above idiom is incorrect.  
  27.  
  28. Given that you do not specify your implementation, such as telling us
  29. what pointer comparisons you use or don't use, its hard to say
  30. whether your example does or doesn't work under all current C++
  31. implementations.  In the absence of evidence, I will hypothesize
  32. the opposite, namely that your class DOESN'T work under all current
  33. C++ implementations.
  34.  
  35. Below find an example that frequently catches programmers off guard.
  36. Again, C++ pointers DO NOT model any notion of object identity.  If you
  37. want your objects to maintain a notion of object identity [A good
  38. idea, IMHO] then you must explicitly GIVE your objects a notion of object
  39. identity.  C++ comes with no such notion built-in.
  40.  
  41. #include <iostream.h>
  42.  
  43. class A { char bsPadding; };
  44. class B : public A {};
  45. class C : public A {};
  46. class D : public B, public C {};
  47.  
  48. main()
  49. {
  50.     D d;
  51.  
  52.     B* pb = &d;
  53.     C* pc = &d;
  54.     A* pa1 = pb; 
  55.     A* pa2 = pc; 
  56.  
  57.     if (pa1 == pa2)
  58.         cout << "pointers match\n";
  59.     else 
  60.         cout << "pointers don't match -- even though of the same"
  61.             " type and 'to the same object'!\n";
  62.  
  63.     return 0;
  64. };
  65.  
  66.