Topic: dynamic_cast<void *> during construction?
Author: "Christopher M. Gurnee" <gurnec@nospam.yahoo.com>
Date: 1998/05/19 Raw View
A.Stokes@globalcomm.co.uk wrote in message
<6jhjqe$35u$1@nnrp1.dejanews.com>...
>What is dynamic_cast<void *>(this) (which returns the address of the
base of
>the complete object) supposed to do while the complete object is
still being
>constructed?
While a ctor of an object is being executed, this object is considered
a most derived object with the type of the currently executing ctor.
In other words, dynamic_cast<void*>(this) == this. CD2 12.7/5 (too
cheap to buy the FDIS yet :) )
-Chris Gurnee
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: A.Stokes@globalcomm.co.uk
Date: 1998/05/15 Raw View
What is dynamic_cast<void *>(this) (which returns the address of the base of
the complete object) supposed to do while the complete object is still being
constructed?
Consider the following example:
struct A
{
virtual ~A() {}
int bogus;
};
struct B
{
virtual ~B() { }
B() { cout << "Object is at " << dynamic_cast<void *>(this); }
int bogus;
};
struct C : public A, public B
{
C() { cout << "C is " << (void *) this
<< "B is " << (void *) (B *) this; }
}
int main()
{
C c;
return 0;
}
Should B's constructor give the address of the C object or the B sub-object?
Initially I expected it to be the C (that's the complete object), then I
thought it should be the B (because when B's constructor runs there isn't a
complete C object yet), and now I'm just confused.
- Alan
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/05/16 Raw View
A.Stokes@globalcomm.co.uk wrote:
>
> What is dynamic_cast<void *>(this) (which returns the address of the
> base of the complete object) supposed to do while the complete object
> is still being constructed?
I don't know what the standard requires, but my understanding of the
usual mechanism by which that cast is implemented leads me to believe
that it will return the address of the largest enclosing object whose
constructor has started running. This is because each level's
constructor begins by making the vtbl pointer point to the vtbl for that
constructor's class, and it is through the vtbl that dynamic_cast finds
out what the enclosing type is and how to find the most derived object.
--
Ciao,
Paul
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]