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: Must derived class reserve space for an empty base class?
- Message-ID: <1992Dec24.202339.26224@microsoft.com>
- Date: 24 Dec 92 20:23:39 GMT
- Organization: Microsoft Corporation
- References: <1992Dec16.202800.3398@microsoft.com> <5450@holden.lulea.trab.se> <85744@ut-emx.uucp>
- Lines: 55
-
- In article <85744@ut-emx.uucp> jamshid@ut-emx.uucp (Jamshid Afshar) writes:
- |The fact the following idiom works under all current C++
- |implementations, and that it will probably continue to work no matter
- |what ANSI says, is what most worries me and why I originally posted
- |this question. Programmers will continue to do things like
- | class Register {
- | Set<Register*> alive;
- | protected:
- | Register() { alive += this; }
- | virtual ~Register() { alive -= this; }
- | static void foo(); // do something to all registered objects
- | };
- |
- |If a special-case rule is not added making the above safe on *any* C++
- |implementation, I hope more C++ literature will point out the fact
- |that the above idiom is incorrect.
-
- Given that you do not specify your implementation, such as telling us
- what pointer comparisons you use or don't use, its hard to say
- whether your example does or doesn't work under all current C++
- implementations. In the absence of evidence, I will hypothesize
- the opposite, namely that your class DOESN'T work under all current
- C++ implementations.
-
- Below find an example that frequently catches programmers off guard.
- Again, C++ pointers DO NOT model any notion of object identity. If you
- want your objects to maintain a notion of object identity [A good
- idea, IMHO] then you must explicitly GIVE your objects a notion of object
- identity. C++ comes with no such notion built-in.
-
- #include <iostream.h>
-
- class A { char bsPadding; };
- class B : public A {};
- class C : public A {};
- class D : public B, public C {};
-
- main()
- {
- D d;
-
- B* pb = &d;
- C* pc = &d;
- A* pa1 = pb;
- A* pa2 = pc;
-
- if (pa1 == pa2)
- cout << "pointers match\n";
- else
- cout << "pointers don't match -- even though of the same"
- " type and 'to the same object'!\n";
-
- return 0;
- };
-
-