J
Juha Nieminen
Assume I have a class Base, and then this:
struct Derived: public Base
{
AnotherClass member;
};
Also assume that I'm allocating an object of type 'Derived' with a
custom allocator and that, for whatever reason, I don't want to call the
constructor of 'Derived', but instead I want to build the object by
first making a placement new for the 'Base' and then another for the
'member'. (Yes, I know this might sound like a bit of a silly thing to
want, but just humor me, please.) Is it valid to do that?
The code would be something like this:
Derived* ptr = allocator.allocate(1);
new (static_cast<Base*>(ptr)) Base(baseParameters);
new (&ptr->member) AnotherClass(otherParameters);
This compiles (and doesn't crash), but is it valid?
struct Derived: public Base
{
AnotherClass member;
};
Also assume that I'm allocating an object of type 'Derived' with a
custom allocator and that, for whatever reason, I don't want to call the
constructor of 'Derived', but instead I want to build the object by
first making a placement new for the 'Base' and then another for the
'member'. (Yes, I know this might sound like a bit of a silly thing to
want, but just humor me, please.) Is it valid to do that?
The code would be something like this:
Derived* ptr = allocator.allocate(1);
new (static_cast<Base*>(ptr)) Base(baseParameters);
new (&ptr->member) AnotherClass(otherParameters);
This compiles (and doesn't crash), but is it valid?