Topic: Shouldn't "this" be the same address


Author: "Bob Sanford" <Bob.Sanford@Compaq.com>
Date: 1997/10/11
Raw View
In the following code, I have a class decended from a base class. Both
constructors display the address of "this" pointer. Shouldn't they be the
same, since it's the same object? If I remove the "virtual" from the
inheritance, it works the way I expect, but the way I have it the addresses
are different. Does anyone know why? I haven't tried this with any other
compiler, I'm using Borland C++ 5.02.

Thanks,
Bob.Sanford@Compaq.com

Here's my code:

#include <iostream.h>

class BaseClass
{
  public:
    BaseClass() { cout << this << endl; }
};


class Foo : virtual public BaseClass
{
  public:
    Foo() { cout << this << endl; }
};


int main()
{
  Foo();
  return 0;
}
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/10/11
Raw View
"Bob Sanford" <Bob.Sanford@Compaq.com> writes:

>In the following code, I have a class decended from a base class. Both
>constructors display the address of "this" pointer.

To be pedantic, they actually display the _value_ of the "this" pointer.

>Shouldn't they be the same, since it's the same object?

No.  The reason is that the pointers are pointers of different types.
There is hence no guarantee that their values when converted to
`void *' will be equal.  [Note that the call to `cout << this'
does an implicit conversion to `void *'.]

Your example is similar to this one:

 class Base {};
 class Derived : public virtual Base {} d;
 cout << (void *) &d;
 cout << (void *) (Base *) &d;

In general, converting a base class pointer to `void *' will give
you the address of the base class sub-object, which is not necessarily
the same as the address of the complete object.  If you want to get
the address of the complete object, use `dynamic_cast<void *>'.
For instance, if you use

 cout << dynamic_cast<void *>(this);

rather than

 cout << this;

in your example, then the values printed out in the two cases should be equal.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]