home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: virtual madness - problem with virtual base class
- Message-ID: <1992Jul25.155730.19131@taumet.com>
- Organization: TauMetric Corporation
- References: <1992Jul24.190709.10315@walter.bellcore.com>
- Date: Sat, 25 Jul 1992 15:57:30 GMT
- Lines: 36
-
- ik@snoopy.ctt.bellcore.com (Ik Su Yoo) writes:
-
- >I'm using cfront 2.1 with ObjectCenter 1.1 and having problem with the
- >following code: [ condensed ]
-
- > class A {
- > public:
- > virtual void f();
- > };
-
- > class B: public virtual A { };
-
- > B bb;
- > void* bbp = &bb;
- > ((B*)bbp)->f();
- > ((A*)bbp)->f();
-
- Pointer casts in C++ do not in general mean "copy the bits". When a
- class hierarchy is involved, the compile-time type is taken into
- account to adjust the pointer value. When you cast &bb to type void*,
- you lose all the compile-time type information.
-
- For example, when a B* is cast to an A*, there is a value change.
- The A base class does not start at the same address as the B derived
- class -- a virtual base must be located somewhere else. The compiler
- generates code to adjust the address to the start of the A object.
-
- In your example, you take the address of a B, cast it to void*. Then
- you take the void* and cast it to type A*. The information that the
- address is really of a B was lost to the compiler, so it can't make the
- adjustment. You take the address of the B and try to use it as though
- it were the address of an A, which it is not.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-