class Graph {
private:
int n;
Head* array;
Graph(int n)
{
this->n = n;
array = new Head[n];
for (int i = 0; i < n; i++)
{
array[i].head->vertex = i;
}
}
};
When the first Head* array is being declared, it is being done in the stack. Then again it is being allocated to the heap using the new keyword inside the constructor.
I want to understand if this is the same as if we wrote: Head* array = new Head[n];
And if so then is this a standard way of declaring member variables in a class? Is it also because when objects are created constructors are invoked first, so member variables are not set if they are not in the constructor?