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

  1. Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Must derived class reserve space for an empty base class?
  5. Summary: does anyone understand my point?
  6. Message-ID: <85825@ut-emx.uucp>
  7. Date: 26 Dec 92 07:31:46 GMT
  8. References: <5450@holden.lulea.trab.se> <85744@ut-emx.uucp> <1992Dec24.202339.26224@microsoft.com>
  9. Reply-To: jamshidemx.utexas.edu
  10. Organization: The University of Texas at Austin; Austin, Texas
  11. Lines: 48
  12.  
  13. In article <1992Dec24.202339.26224@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
  14. |In article <85744@ut-emx.uucp> jamshid@ut-emx.uucp (Jamshid Afshar) writes:
  15. ||[...] Programmers will continue to do things like
  16. ||    class Register {
  17. ||       Set<Register*> alive;
  18.         ^woops, I forgot 'static'
  19. ||    protected:
  20. ||       Register() { alive += this; }
  21. ||       virtual ~Register() { alive -= this; }
  22. ||       static void foo();  // do something to all registered objects
  23. ||    };
  24.  
  25. |Given that you do not specify your implementation, such as telling us
  26. |what pointer comparisons you use or don't use, its hard to say
  27. |whether your example does or doesn't work under all current C++
  28. |implementations.
  29.  
  30. I forgot the keyword 'static' in my example, but I thought the
  31. intentions were obvious.  The problem with the above idiom is that
  32. Register() could add the same 'Register*' twice (the second add being
  33. ignored) when constructing a Register-derived object that contains
  34. another Register-derived object as its first data member.
  35.  
  36. All current and probably future C++ compilers use a hidden vtbl
  37. pointer to implement virtual functions so you probably won't encounter
  38. any problems with the above idiom, but it is "wrong" unless rules are
  39. changed.
  40.  
  41. |Below find an example that frequently catches programmers off guard.
  42. |Again, C++ pointers DO NOT model any notion of object identity. [...]
  43. |class A { char bsPadding; };
  44. |class B : public A {};
  45. |class C : public A {};
  46. |class D : public B, public C {};
  47.     A* pa1 = (B*)&d;    A* pa2 = (C*)&d;
  48.     if (pa1==pa2)
  49. |        cout << "pointers match\n";
  50. |    else 
  51. |        cout << "pointers don't match -- even though of the same"
  52. |            " type and 'to the same object'!\n";
  53.  
  54. I know this and I don't have a problem with it.  There are two
  55. different A objects (they would each have separate data members) so I
  56. don't think they are pointers "to the same object".
  57.  
  58. Jamshid Afshar
  59. jamshid@emx.utexas.edu
  60.  
  61.