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

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Pointer comparisons
  5. Message-ID: <1992Dec24.200840.25856@microsoft.com>
  6. Date: 24 Dec 92 20:08:40 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec17.151642.9954@bcrka451.bnr.ca> <1992Dec19.001851.22116@microsoft.com> <1992Dec22.140212.12579@bcrka451.bnr.ca>
  9. Lines: 49
  10.  
  11. In article <1992Dec22.140212.12579@bcrka451.bnr.ca> sjm@bcrki65.bnr.ca (Stuart MacMartin) writes:
  12. |2.  Perhaps there is a philosophical argument that might resolve this issue.
  13. |    If two objects have the same interface but no state (or constant state), and
  14. |    they have the same lifetime, are they in fact the same object?  Or, perhaps,
  15. |    can they be treated as if they are the same object?
  16.  
  17. Here's a simple counterexample. b and b.a are clearly different objects.
  18. They have different type, and they respond differently to a given method
  19. call.  
  20.  
  21. HOWEVER, they can both be legally used to initialized an A*, and
  22. in which case the pointers compare equal, and the two different objects
  23. THEN respond the same to a given method.
  24.  
  25.  
  26. #include <iostream.h>
  27.  
  28. class A
  29. {
  30. public:
  31.     static void print() { cout << "A\n"; }
  32. };
  33.  
  34. class B : public A
  35. {
  36. public:
  37.     A a;
  38.     static void print() { cout << "B\n"; }
  39. };
  40.  
  41. main()
  42. {
  43.     B b;
  44.     b.print();
  45.     b.a.print();
  46.  
  47.     A* pb = &b;
  48.     A* pa = &(b.a);
  49.  
  50.     if (pa == pb)
  51.         cout << "addresses match\n";
  52.     else
  53.         cout << "addresses don't match\n";
  54.  
  55.     pb->print();
  56.     pa->print();
  57.  
  58.     return 0;    
  59. }
  60.