P
Petroz
Is the following legal:
1/ Calling placement new for a different type that is guaranteed to
have the same member data.
2/ Calling placement new multiple times on the same memory.
example:
struct StateFuctorBase
{
virtual void execute() { throw; }
void *Data;
}
struct StateFunctorA : public StateFuctorBase
{
void execute() { std::cout << "StateFunctorA executed"; }
}
struct StateFunctorB : public StateFuctorBase
{
void execute() { std::cout << "StateFunctorB executed"; }
}
main()
{
StateFunctorBase base;
new (&base) StateFunctorA();
base.execute();
new (&base) StateFunctorB();
base.execute();
}
1/ Calling placement new for a different type that is guaranteed to
have the same member data.
2/ Calling placement new multiple times on the same memory.
example:
struct StateFuctorBase
{
virtual void execute() { throw; }
void *Data;
}
struct StateFunctorA : public StateFuctorBase
{
void execute() { std::cout << "StateFunctorA executed"; }
}
struct StateFunctorB : public StateFuctorBase
{
void execute() { std::cout << "StateFunctorB executed"; }
}
main()
{
StateFunctorBase base;
new (&base) StateFunctorA();
base.execute();
new (&base) StateFunctorB();
base.execute();
}