Declaring and initializing the table member


I want to make vertices an array of Vector. But I don't know how to declare and initialize it. I tried this, but it complains:

class Mesh {
    Vector  vertices[];
    int     verticesCount;
    virtual ~Mesh();

Mesh::Mesh() {
    verticesCount = 4;

    vertices = new Vector[verticesCount]; // error: expected primary-expression before ']' token
    vertices[0] = new Vector(0, 0, 0);
    vertices[1] = new Vector(1, 0, 0);
    vertices[2] = new Vector(1, 1, 0);
    vertices[3] = new Vector(0, 1, 0);

Mesh::~Mesh() {
   delete vertices;


Trying to correct, applying your tips, I reach this:

Vector*     vertices;
vertices = new Vector[verticesCount];
vertices[0] = Vector(0, 0, 0);
vertices[1] = Vector(1, 0, 0);
vertices[2] = Vector(1, 1, 0);
vertices[3] = Vector(0, 1, 0);
delete[] vertices;

And it worked. But is it ok?

You declare vertices as an unspecified array of Vector, then you try to allocate memory for each entry in the array.

First C++ doesn't support empty arrays (if I remember correct), and the data type of the array is not pointers to Vector which means you can not use the new expressions.

If you want to use a dynamic array of vectors, please use std::vector instead:

std::vector<Vector> vertices;

And in the constructor:

vertices.push_back(Vector(0, 0, 0));
// etc.