0

I have an array of elements of type determined by template:
SomeType array[10];
I have pointer to required class object:
RequiredType * objectList;
I do assignment:
objectList = (RequiredType)array;

How can I call RequiredType() constructor to construct an object at pointer location where memory is already allocated? This question also applies to any situation where you have allocated mamory at which you have to cnstruct an object.

  • Because I cannot use new in this case and I already have allocated memory. At that one location diferent objects will be initialized. It's an embedded system, that is why I need this. – Karolis Milieška Feb 04 '16 at 09:21
  • @KarolisMilieška: In that case, a `union` might be the better solution. Placement new isn't going to fix alignment, it requires that **you** provide a suitably aligned address. A `union` forces the **compiler** to work out the alignment. – MSalters Feb 04 '16 at 12:18

0 Answers0