home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
- From: jamshid@ut-emx.uucp (Jamshid Afshar)
- Newsgroups: comp.std.c++
- Subject: Re: Must derived class reserve space for an empty base class?
- Summary: does anyone understand my point?
- Message-ID: <85825@ut-emx.uucp>
- Date: 26 Dec 92 07:31:46 GMT
- References: <5450@holden.lulea.trab.se> <85744@ut-emx.uucp> <1992Dec24.202339.26224@microsoft.com>
- Reply-To: jamshidemx.utexas.edu
- Organization: The University of Texas at Austin; Austin, Texas
- Lines: 48
-
- In article <1992Dec24.202339.26224@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
- |In article <85744@ut-emx.uucp> jamshid@ut-emx.uucp (Jamshid Afshar) writes:
- ||[...] Programmers will continue to do things like
- || class Register {
- || Set<Register*> alive;
- ^woops, I forgot 'static'
- || protected:
- || Register() { alive += this; }
- || virtual ~Register() { alive -= this; }
- || static void foo(); // do something to all registered objects
- || };
-
- |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.
-
- I forgot the keyword 'static' in my example, but I thought the
- intentions were obvious. The problem with the above idiom is that
- Register() could add the same 'Register*' twice (the second add being
- ignored) when constructing a Register-derived object that contains
- another Register-derived object as its first data member.
-
- All current and probably future C++ compilers use a hidden vtbl
- pointer to implement virtual functions so you probably won't encounter
- any problems with the above idiom, but it is "wrong" unless rules are
- changed.
-
- |Below find an example that frequently catches programmers off guard.
- |Again, C++ pointers DO NOT model any notion of object identity. [...]
- |class A { char bsPadding; };
- |class B : public A {};
- |class C : public A {};
- |class D : public B, public C {};
- A* pa1 = (B*)&d; A* pa2 = (C*)&d;
- 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";
-
- I know this and I don't have a problem with it. There are two
- different A objects (they would each have separate data members) so I
- don't think they are pointers "to the same object".
-
- Jamshid Afshar
- jamshid@emx.utexas.edu
-
-