Consider a simple class:
class SimpleClass {
int a;
public:
SimpleClass():a(0){}
SimpleClass(int n):a(n){}
// other functions
};
SimpleClass *p1, *p2;
p1 = new SimpleClass[5];
p2 = new SimpleClass(3);
In this case the default constructor SimpleClass()
gets called for the construction of newly allocated objects for p1 and the parameterized constructor for p2. My question is: Is it possible to allocate an array and use the parameterized constructor using the new operator? For example, if I want the array to be initialized with objects having variable a
value being 10, 12, 15, ... respectively, is it possible to pass these values when using the new operator?
I know that using stl vector is a better idea to deal with arrays of objects. I want to know whether the above is possible using new to allocate an array.