home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c++
- Path: sparky!uunet!microsoft!hexnut!jimad
- From: jimad@microsoft.com (Jim Adcock)
- Subject: Re: Pointer comparisons
- Message-ID: <1992Dec24.200840.25856@microsoft.com>
- Date: 24 Dec 92 20:08:40 GMT
- Organization: Microsoft Corporation
- References: <1992Dec17.151642.9954@bcrka451.bnr.ca> <1992Dec19.001851.22116@microsoft.com> <1992Dec22.140212.12579@bcrka451.bnr.ca>
- Lines: 49
-
- In article <1992Dec22.140212.12579@bcrka451.bnr.ca> sjm@bcrki65.bnr.ca (Stuart MacMartin) writes:
- |2. Perhaps there is a philosophical argument that might resolve this issue.
- | If two objects have the same interface but no state (or constant state), and
- | they have the same lifetime, are they in fact the same object? Or, perhaps,
- | can they be treated as if they are the same object?
-
- Here's a simple counterexample. b and b.a are clearly different objects.
- They have different type, and they respond differently to a given method
- call.
-
- HOWEVER, they can both be legally used to initialized an A*, and
- in which case the pointers compare equal, and the two different objects
- THEN respond the same to a given method.
-
-
- #include <iostream.h>
-
- class A
- {
- public:
- static void print() { cout << "A\n"; }
- };
-
- class B : public A
- {
- public:
- A a;
- static void print() { cout << "B\n"; }
- };
-
- main()
- {
- B b;
- b.print();
- b.a.print();
-
- A* pb = &b;
- A* pa = &(b.a);
-
- if (pa == pb)
- cout << "addresses match\n";
- else
- cout << "addresses don't match\n";
-
- pb->print();
- pa->print();
-
- return 0;
- }
-