Array of objects with no default constructor

less than 1 minute read

If you want to have an array of objects which have no default constructor, it’s really hard to do with arrays. Best way is to use vectors. Because unlike arrays, vectors let us choose which constructor to be used when creating objects. Here is the example:

Let ClassA takes ClassC as arguement.

ClassA.h

class ClassA {
public:
	ClassA(ClassC *pointer_to_ClassC);
};

In ClassB, declare the vector.

ClassB.h

class ClassB {
private:
	std::vector<ClassA> myVector;
};

In source code, I first initializing the vector with n instances directly by calling the constructor of vector, however it didn’t help to create n instances of ClassA. It works if you push items one by one to the vector.

ClassB.cpp

ClassC *ClassCInstance = new ClassC();
for (i = 0; i < 10; ++i) {
     myVector.push_back(ClassA(ClassCInstance));
}

Leave a Comment