Why do I have to provide default ctor? - c++

Why do I have to provide default ctor if I want to create an array of objects of my type?
Thanks for answers

Because they have to be initialized.
Consider if it wasn't the case:
struct foo
{
foo(int) {}
void bar(void) {}
};
foo a[10];
foo f = a[0]; // not default-constructed == not initialized == undefined behavior
Note you don't have to:
int main(){
// initializes with the int constructor
foo a[] = {1, 2, 3};
}
// if the constructor had been explicit
int main(){
// requires copy-constructor
foo a[] = {foo(1), foo(2), foo(3)};
}
If you really need an array of objects and you can't give a meaningful default constructor, use std::vector.
If you really need an array of of objects, can't give a meaningful default constructor, and want to stay on the stack, you need to lazily initialize the objects. I have written such a utility class. (You would use the second version, the first uses dynamic memory allocation.)
For example:
typedef lazy_object_stack<foo> lazy_foo;
lazy_foo a[10]; // 10 lazy foo's
for (size_t i = 0; i < 10; ++i)
{
// create a foo, on the stack, passing `i` to the constructor
a[i].create(i);
}
for (size_t i = 0; i < 10; ++i)
a[i].get().bar(); // and use it
// automatically destructed, of course

The default constructor will be called for each object in the array.
You don't have to specify a default constructor as one will be created for you.
Just make sure you don't declare a constructor with no parameters as private or protected.
Here's an example of one being created for you:
class C
{
};
C c[10];
Whereas if you make it private you get a compiling error:
class C
{
private:
C()
{
}
};
C c[10];

When defining an array, you cannot specify ctor parameter, hence every object in the array must be constructed using the default ctor.
Normally, the C++ compiler will create the default ctor for you automatically. But, if you define a ctor with parameters, then the automatically creation of the default ctor is supressed, and you must explicitly write one.

Because when the array is initialized, default constructors are called for its items.

Related

How to call parameterized constructor of member object variable in a class' default constructor in C++?

I want to initialize member object variables in the default constructor of the class.
Let's consider the following,
class ABC {
ABC(int A, int B) {
a = A;
b = B;
}
int a;
int b;
};
class Foo {
Foo();
ABC m_obj1;
};
From the above example, I would like to initialize "obj1" in "Foo::Foo()".
One of the restrictions I have is that I cannot do so in the initializer list, as I need to do some computation before I could initialize the member. So the option available (ASFAIK) is to do so in the body of the default constructor only.
Any inputs, how could I do this?
Edit: Restricting to C++11
Would this be a correct way,
Foo:Foo() {
int x = 10;
int y = 100;
m_Obj1(x, y); //Is this correct? <--------
}
Depending on your exact problem and requirements, multiple solutions might be available:
Option 1: Use a function to do the computations and call Foo constructor
Foo makeFoo()
{
// Computations here that initialize A and B for obj1 constructor
return Foo(A, B)
}
Option 2: Call a function that does the computations and initialize obj1 in Foo member initializer list
ABC initABC() {
// Some computations
return ABC(A, B)
}
Foo() : obj1(initABC()) {}
Option 3: Dynamically allocate obj1, for instance with a std::unique_ptr
Option 4: Use std::optional or an emulated c++11 version as shown by other answers
You simply call the base constructor inside the initializer list of the derived constructor. The initializer list starts with ":" after the parameters of the constructor. See example code!
There is no problem to call functions inside the initializer list itself.
int CallFunc1(int x) { return x*2; }
int CallFunc2(int y) { return y*4; }
class ABC {
public:
ABC(int A, int B):a{CallFunc1(A)},b{CallFunc2(B)} {
std::cout << "Constructor with " << a << " " << b << " called" << std::endl;
}
private:
int a;
int b;
};
class Foo {
public:
Foo(): obj1(1,2){}
Foo( int a, int b): obj1(a, b){}
private:
ABC obj1;
};
int main()
{
Foo foo;
Foo fooo( 9,10);
}
edit:
The best method I can think of for your case is a copy constructor, being more specific on what you need to store helps a lot since if it is just two ints inside a class dynamic allocation is not worth it, the size of the object being constructed makes a difference to what method is best, copy constructors can be slower with much larger data types as the object has to be created twice: once when it is automatically constructed in the parent objects constructor and again when a temporary object is created and all the values have to be copied, which can be slower then dynamically allocating if the size is larger.
As far as I'm aware all objects in a class are automatically initialized/allocated in the constructor so sadly dynamic memory use may be your best bet here.
If you are fine with having the object initialized but empty so you know it is not 'ready' yet you can later fill it with useful data when you would have wanted to initialize it. This can be done with default constructors that set the things inside the object to null values or something similar so you know the object hasn't been properly initialized yet. Then before using the object you can check whether it has been initialized by checking for the null values or by having put a bool in the object that tells you whether it is initialized. Dynamically allocated would still be better in my opinion and makes the code look cleaner overall as all you need to store is a null pointer until the object is needed and then allocated and set to the pointer. It is also very easy to check if the pointer is equal to nullptr to know the state of your object.
Dynamically allocating memory may be a hassle since you have to make sure to get rid of memory leaks and it is slightly slower than using the stack, but it is a necessary skill for c++ since the stack is not enough when making programs that use more than the few available megabytes of data on the stack so if you are doing this simply to avoid the hassle I recommend learning it properly. It would be nice if you could be more specific about what kind of object you want to do this with or if you just want an answer that works for most cases.
eg:
*ABC obj1 = nullptr;
...object is needed
obj1 = new(ABC(constructor stuff));
...obj1 isn't needed
delete obj1;
or c++ automatically deletes it when the program closes.

Passing arguments to the constructor of an array [duplicate]

If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
class Foo{
public:
Foo(int i) {}
}
How to declare an array or a vector which contains 10000 Foo objects?
For an array you would have to provide an initializer for each element of the array at the point where you define the array.
For a vector you can provide an instance to copy for each member of the vector.
e.g.
std::vector<Foo> thousand_foos(1000, Foo(42));
Actually, you can do it as long you use an initialization list, like
Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };
however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.
sbi had the best answer for plain arrays, but didn't give an example. So...
You should use placement new:
char *place = new char [sizeof(Foo) * 10000];
Foo *fooArray = reinterpret_cast<Foo *>(place);
for (unsigned int i = 0; i < 10000; ++i) {
new (fooArray + i) Foo(i); // Call non-default constructor
}
Keep in mind that when using placement new, you are responsible for calling the objects' destructors -- the compiler won't do it for you:
// In some cleanup code somewhere ...
for (unsigned int i = 0; i < 10000; ++i) {
fooArray[i].~Foo();
}
// Don't forget to delete the "place"
delete [] reinterpret_cast<char *>(fooArray);
This is about the only time you ever see a legitimate explicit call to a destructor.
NOTE: The first version of this had a subtle bug when deleting the "place". It's important to cast the "place" back to the same type that was newed. In other words, fooArray must be cast back to char * when deleting it. See the comments below for an explanation.
You'd need to do an array of pointers to Foo.
Foo* myArray[10000];
for (int i = 0; i < 10000; ++i)
myArray[i] = new Foo(i);
When you declare an object that has no default constructor, you must initialize it in the declaration.
Foo a; // not allowed
Foo b(0); // OK
The same goes for arrays of such types:
Foo c[2]; // not allowed
Foo d[2] = { 0, 1 }; // OK
Foo e[] = { Foo(0), Foo(1), Foo(2) }; // also OK
In your case, you'll probably find it impractical to initialize all 10,000 elements like that, so you might wish to rethink whether the class really shouldn't have a default constructor.
The only way to define an array of a class with no default constructor would be to initialize it right away - not really an option with 10000 objects.
You can, however, allocate enough raw memory whichever way you want and use placement new to create the objects in that memory. But if you want to do this, it's better to use std::vector which does exactly that:
#include <iostream>
#include <vector>
struct foo {
foo(int) {}
};
int main()
{
std::vector<foo> v;
v.resize(10000,foo(42));
std::cout << v.size() '\n';
return 0;
}
You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}
Foo array[10000] = { 1, 2, 3, ..., 10000 };
Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.
class single
{
int data;
public:
single()
{
data = 0;
}
single(int i)
{
data = i;
}
};
// in main()
single* obj[10000];
for (unsigned int z = 0; z < 10000; z++)
{
obj[z] = new single(10);
}
Another option might be to use an array of boost::optional<Foo>:
boost::optional<Foo> foos[10]; // No construction takes place
// (similar to vector::reserve)
foos[i] = Foo(3); // Actual construction
One caveat is that you'll have to access the elements with pointer syntax:
bar(*foos[2]); // "bar" is a function taking a "Foo"
std::cout << foos[3]->baz(); // "baz" is a member of "Foo"
You must also be careful not to access an unitialized element.
Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.
In straight C, using int foo[10000] = {1}; will initialize the first array item to 1 and the remainder of the array to zero. Does C++ not auto-initialize unspecified array members, or does this require a default constructor?
If it makes sense for your class, you could provide a default value for your constructor parameter:
class Foo
{
public:
explicit Foo(int i = 0);
}
Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)
I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor

Array of Objects with Parameterised Constructors [duplicate]

If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
class Foo{
public:
Foo(int i) {}
}
How to declare an array or a vector which contains 10000 Foo objects?
For an array you would have to provide an initializer for each element of the array at the point where you define the array.
For a vector you can provide an instance to copy for each member of the vector.
e.g.
std::vector<Foo> thousand_foos(1000, Foo(42));
Actually, you can do it as long you use an initialization list, like
Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };
however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.
sbi had the best answer for plain arrays, but didn't give an example. So...
You should use placement new:
char *place = new char [sizeof(Foo) * 10000];
Foo *fooArray = reinterpret_cast<Foo *>(place);
for (unsigned int i = 0; i < 10000; ++i) {
new (fooArray + i) Foo(i); // Call non-default constructor
}
Keep in mind that when using placement new, you are responsible for calling the objects' destructors -- the compiler won't do it for you:
// In some cleanup code somewhere ...
for (unsigned int i = 0; i < 10000; ++i) {
fooArray[i].~Foo();
}
// Don't forget to delete the "place"
delete [] reinterpret_cast<char *>(fooArray);
This is about the only time you ever see a legitimate explicit call to a destructor.
NOTE: The first version of this had a subtle bug when deleting the "place". It's important to cast the "place" back to the same type that was newed. In other words, fooArray must be cast back to char * when deleting it. See the comments below for an explanation.
You'd need to do an array of pointers to Foo.
Foo* myArray[10000];
for (int i = 0; i < 10000; ++i)
myArray[i] = new Foo(i);
When you declare an object that has no default constructor, you must initialize it in the declaration.
Foo a; // not allowed
Foo b(0); // OK
The same goes for arrays of such types:
Foo c[2]; // not allowed
Foo d[2] = { 0, 1 }; // OK
Foo e[] = { Foo(0), Foo(1), Foo(2) }; // also OK
In your case, you'll probably find it impractical to initialize all 10,000 elements like that, so you might wish to rethink whether the class really shouldn't have a default constructor.
The only way to define an array of a class with no default constructor would be to initialize it right away - not really an option with 10000 objects.
You can, however, allocate enough raw memory whichever way you want and use placement new to create the objects in that memory. But if you want to do this, it's better to use std::vector which does exactly that:
#include <iostream>
#include <vector>
struct foo {
foo(int) {}
};
int main()
{
std::vector<foo> v;
v.resize(10000,foo(42));
std::cout << v.size() '\n';
return 0;
}
You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}
Foo array[10000] = { 1, 2, 3, ..., 10000 };
Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.
class single
{
int data;
public:
single()
{
data = 0;
}
single(int i)
{
data = i;
}
};
// in main()
single* obj[10000];
for (unsigned int z = 0; z < 10000; z++)
{
obj[z] = new single(10);
}
Another option might be to use an array of boost::optional<Foo>:
boost::optional<Foo> foos[10]; // No construction takes place
// (similar to vector::reserve)
foos[i] = Foo(3); // Actual construction
One caveat is that you'll have to access the elements with pointer syntax:
bar(*foos[2]); // "bar" is a function taking a "Foo"
std::cout << foos[3]->baz(); // "baz" is a member of "Foo"
You must also be careful not to access an unitialized element.
Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.
In straight C, using int foo[10000] = {1}; will initialize the first array item to 1 and the remainder of the array to zero. Does C++ not auto-initialize unspecified array members, or does this require a default constructor?
If it makes sense for your class, you could provide a default value for your constructor parameter:
class Foo
{
public:
explicit Foo(int i = 0);
}
Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)
I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor

Why does an array of user defined type have to call the default constructor thereof?

Please consider the following example
struct Foo
{
int bar;
Foo(int i):bar(i){cout << "real ctor\n";}
Foo(){cout << "default ctor\n";}
};
int main()
{
Foo fooArr[3];//default ctor called 3 times
for(int i=0;i!=3;++i)cout << fooArr[i].bar << endl;//bare memory junk
cout << endl;
vector<Foo> fooVec;
for(int i=0;i!=3;++i){
fooVec.push_back(Foo(i)); //only real ctor called
cout << fooVec[i].bar << endl;//real thing
}
cout << endl;
int iArr[3];
for(int i=0;i!=3;++i)cout << iArr[i] << endl;//bare memory junk
}
I don't want any user of Foo to call its default constructor, because it's not in my design. But I'd like my users to be able to use an array of Foo, to support that, I was forced to provide a pointless and confusing Foo::Foo(). I just don't understand why does the C++ standard force programmers to do such a thing. What is the rationale behind it? Why the inconsistency? Could any of you smart guys who get this explain it to me, please? Thanks in advance!
You can make arrays of Foo even if it doesn't have a default constructor. It's just that the elements have to be constructed when you declare the array. So you can do this:
Foo fooArr[] = { Foo( 1 ), Foo( 2 ), Foo( 3 ) };
The alternative is to use a a dynamic array (your vector<Foo> example, which is probably best) or an array of pointers to Foo (like shared_ptr<Foo> arrFoo[3])
shared_ptr<Foo> arrFoo[3];
arrFoo[2].reset( new Foo(3) );
A final note about vector<Foo>: since the size of the array is known in advance, you can improve performance by reserving enough space in the vector for all future Foos:
vector<Foo> arrFoo;
arrFoo.reserve( 3 );
for( int i = 0; i<3; ++i )
arrFoo.push_back( Foo( i ) );
EDIT: Your question was why do you have to have a default constructor to make a static array of the type. I thought the answer was clear but I'll try to explain it.
Foo bar1; Foo bar2; creates two objects using the default constructor, since no arguments were provided.
Foo bar[2]; is essentially the same thing. It declares two objects that need to be constructed. There is no way to declare an object without constructing it - that's the very point of declaring it in the first place.
A static array in C++ is just a bunch of objects placed contiguously memory. It's not a separate object.
Hope that makes sense.
The rationale is that the array is full of default constructed elements, so the type of the elements must be default constructible. If you initialized the array with some values, the default construction wouldn't be required:
Foo fooArr1[3]; // full of default constructed Foos
Foo fooArr[3] = {1,2,3}; // default constructor not required. Foo(int) called.
Note that the second line in the code example uses the implicit conversion from int to Foo provided by the implicit Foo(int) converting constructor.
The reason you have to provide your own default constructor is that you have declared one constructor, which disables the automatic generation of the default constructor. The rationale behind this is that if you need to provide some constructor, it is likely that you also want to do something special in the default constructor.
If you really are worried about user provided constructors, then you can make your class a real aggregate and use aggregate initialization:
struct Foo
{
// no user declared constructors
int foo;
};
int main()
{
Foo fooArr1[3]; // OK
Foo fooArr[3] = { {1}, {2}, {3} }; // aggregate construction
}
In C++11 you can enable the compiler generated default constructor using default:
Foo()=default;
You have to choose: either not defining a default constructor, and therefore, you can't declare an array of Foo. Or declaring a default constructor (empty even) and can declare an array of Foo.
If you have dealt before with OOP languages such as C# or Java, and you have a class Foo and Foo[] arr, then you don't have to declare default constructor, because the array in these languages carries only references (addresses) to objects. The array itself is an object, so arr when created will == null. When using arr = new Foo[3]; then we make a new object of array that contains 3 references: arr == { null, null, null }. Then you assign an object to each reference: for (int i = 0; i < 3; ++i) arr[i] = new Foo(i);.
However, C++ is different because the arrays carry the object itself rather than a reference to it. So, when carrying the object itself, it must have no-parameter constructor to be called with each object. (i.e. in C++: Foo arr[3]; then arr = { objectOfFoo, objectOfFoo, objectOfFoo }
A solution to your problem may found by decalring an array of pointers:
Foo * arr[10] = { 0 }; // arr = { NULL, NULL, NULL, ... , NULL }
for (int i = 0; i < 10; ++i) arr[i] = new Foo(3); // you don't have to declare default constructor
// some using of array
// C++ doesn't have a garbage collector
for (int i = 0; i < 10; ++i) delete arr[i];

How do I declare an array of objects whose class has no default constructor?

If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
class Foo{
public:
Foo(int i) {}
}
How to declare an array or a vector which contains 10000 Foo objects?
For an array you would have to provide an initializer for each element of the array at the point where you define the array.
For a vector you can provide an instance to copy for each member of the vector.
e.g.
std::vector<Foo> thousand_foos(1000, Foo(42));
Actually, you can do it as long you use an initialization list, like
Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };
however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.
sbi had the best answer for plain arrays, but didn't give an example. So...
You should use placement new:
char *place = new char [sizeof(Foo) * 10000];
Foo *fooArray = reinterpret_cast<Foo *>(place);
for (unsigned int i = 0; i < 10000; ++i) {
new (fooArray + i) Foo(i); // Call non-default constructor
}
Keep in mind that when using placement new, you are responsible for calling the objects' destructors -- the compiler won't do it for you:
// In some cleanup code somewhere ...
for (unsigned int i = 0; i < 10000; ++i) {
fooArray[i].~Foo();
}
// Don't forget to delete the "place"
delete [] reinterpret_cast<char *>(fooArray);
This is about the only time you ever see a legitimate explicit call to a destructor.
NOTE: The first version of this had a subtle bug when deleting the "place". It's important to cast the "place" back to the same type that was newed. In other words, fooArray must be cast back to char * when deleting it. See the comments below for an explanation.
You'd need to do an array of pointers to Foo.
Foo* myArray[10000];
for (int i = 0; i < 10000; ++i)
myArray[i] = new Foo(i);
When you declare an object that has no default constructor, you must initialize it in the declaration.
Foo a; // not allowed
Foo b(0); // OK
The same goes for arrays of such types:
Foo c[2]; // not allowed
Foo d[2] = { 0, 1 }; // OK
Foo e[] = { Foo(0), Foo(1), Foo(2) }; // also OK
In your case, you'll probably find it impractical to initialize all 10,000 elements like that, so you might wish to rethink whether the class really shouldn't have a default constructor.
The only way to define an array of a class with no default constructor would be to initialize it right away - not really an option with 10000 objects.
You can, however, allocate enough raw memory whichever way you want and use placement new to create the objects in that memory. But if you want to do this, it's better to use std::vector which does exactly that:
#include <iostream>
#include <vector>
struct foo {
foo(int) {}
};
int main()
{
std::vector<foo> v;
v.resize(10000,foo(42));
std::cout << v.size() '\n';
return 0;
}
You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}
Foo array[10000] = { 1, 2, 3, ..., 10000 };
Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.
class single
{
int data;
public:
single()
{
data = 0;
}
single(int i)
{
data = i;
}
};
// in main()
single* obj[10000];
for (unsigned int z = 0; z < 10000; z++)
{
obj[z] = new single(10);
}
Another option might be to use an array of boost::optional<Foo>:
boost::optional<Foo> foos[10]; // No construction takes place
// (similar to vector::reserve)
foos[i] = Foo(3); // Actual construction
One caveat is that you'll have to access the elements with pointer syntax:
bar(*foos[2]); // "bar" is a function taking a "Foo"
std::cout << foos[3]->baz(); // "baz" is a member of "Foo"
You must also be careful not to access an unitialized element.
Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.
In straight C, using int foo[10000] = {1}; will initialize the first array item to 1 and the remainder of the array to zero. Does C++ not auto-initialize unspecified array members, or does this require a default constructor?
If it makes sense for your class, you could provide a default value for your constructor parameter:
class Foo
{
public:
explicit Foo(int i = 0);
}
Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)
I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor