safe placement new & explicit destructor call
Posted
by uray
on Stack Overflow
See other posts from Stack Overflow
or by uray
Published on 2010-06-07T23:38:49Z
Indexed on
2010/06/07
23:42 UTC
Read the original article
Hit count: 285
this is an example of my codes:
`
template <typename T> struct MyStruct {
T object;
}
template <typename T> class MyClass {
MyStruct<T>* structPool;
size_t structCount;
MyClass(size_t count) {
this->structCount = count;
this->structPool = new MyStruct<T>[count];
for( size_t i=0 ; i<count ; i++ ) {
//placement new to call constructor
new (&this->structPool[i].object) T();
}
}
~MyClass() {
for( size_t i=0 ; i<this->structCount ; i++ ) {
//explicit destructor call
this->structPool[i].object.~T();
}
delete[] this->structPool;
}
}
`
my question is, is this a safe way to do? do I make some hidden mistake at some condition? will it work for every type of object (POD and non-POD) ?
© Stack Overflow or respective owner