Not really adding to much by the way of practicality, but have a look at this...
Code:
#include <iostream>
#include <new>
class Foo {
public:
int number;
Foo(int num) {number = num;}
};
int main()
{
const int nNum = 10;
const int nVal = 2;
//call global operator new[] to alloc
//memory without constructor calls
Foo* ptrFoo = static_cast<Foo*>
(operator new[](nNum*sizeof(Foo)));
//call placement new to call constructors
//of each object in pre-alloced memory
for(int i = 0;i < nNum;i++)
new(ptrFoo+i) Foo(nVal*i);
//Read value of each object...
for(int i = 0;i < nNum;i++){
std::cout << "Foo number " << i+1;
std::cout << " has a value of ";
std::cout << ptrFoo[i].number << std::endl;
}
//Must now make call manual call to destructor..
//maybe not needed, but to make a point
for(int i = 0;i < nNum;i++)
ptrFoo[i].~Foo();
//call operator delete[] to free memory
operator delete[] (ptrFoo);
}
Scott Meyer's books do some really decent coverage on the new operator (the normal new call), operator new (how the memory is provided) and placement new (how constructors in arrays are called).......this shows how you can take a normal new call apart and access the construction of each individual object....
BTW...Meyer's books are awsome and well worth a read.
<edit>Oh...and btw....this compiles ok on Codewarrior and Mingw...but dies a death on VC++6 </edit>