Re: Constructing Derived in shell of Base <shudder>
On 07/15/2010 09:29 PM, Pete Becker wrote:
On 2010-07-15 16:17:44 -0400, Kai-Uwe Bux said:
cpp4ever wrote:
[...]
may I suggest the following
//---------------------
struct B { int j; virtual void f() { std::cout << "B::f\n"; } virtual
~B() {} };
struct D : B { virtual void f() { std::cout << "D::f\n";} };
int main()
{
char obj[sizeof(D)];
B *p = new (obj) B;
p->f(); // B::f() involked
p->~B();
B *p = new (obj) D;
Probably, the above was meant to be:
p = new (obj) D;
Nice idea: assigning a new value to p circumvents [3.8/7] as no old
pointer
to corps is in continued use.
Well, sure. That's the essence of what every memory manager does.
p->f(); // D::f() involked
p->~B(); // D Destructor will be called
}
//---------------------
This still requires care, as before creating an object with new, any
previously created object should be destroyed. Worse still the object
will not automatically be destroyed when it goes out of scope. Therefore
IMHO this sort of functionality should be encapsulated within a class
that ensures things are handled safely.
Just a question: does the standard guarantee that obj is correctly
alligned
for objects of type D and B?
No. It's a char array, with alignment suitable for a char array.
In C++0x you'll get aligned storage with the aligned_storage template:
aligned_storage<sizeof(D), alignof(D)>::type obj;
or with attributes:
unsigned char obj [[ align(alignof(D)) ]] [sizeof(D)];
Does alignment really apply to where a variable is stored? Sure it
affects how member variables will be stored within a struct or class.
Unless I'm mistaken, the suggestion here is that due to alignment the
sizeof operator is only correct for properly aligned variables and the
new (pointer) object operator does not actually create the object at the
address pointed to. Surely this violates what that variant of the new
operator is supposed to do. In practice I've not found this to be the
case, and if it ever does so, IMHO it would be a cause of obscure and
difficult bugs. To the best of my knowledge alignment can be 1,2,4 or 8
byte, and is based on an offset address from the base address of the
object, aligning all of the member variables to a multiple of 1,2,3,4 or
8 as required. Please feel free to correct/expand on this to help
clarify it's understanding.
cpp4ever