I think you're getting confused.
This dynamically allocates a MY_WIDGET object, and requires arguments to the constructor for MY_WIDGET:
arr[x] = new MY_WIDGET(x,y);
This dynamically allocates an array of pointers to MY_WIDGET objects. It does not allocate any actual MY_WIDGET objects, and therefore doesn't ever invoke the constructor for that class:
1 2
|
const int numWidgets = 10;
MY_WIDGET** arr = new MY_WIDGET*[numWidgets];
|
This statically creates a similar array:
1 2
|
const int numWidgets = 10;
MY_WIDGET* arr[numWidgets];
|
Again, the MY_WIDGET constructor is not invoked.
Some things to note:
1) In C++, it's illegal to try and statically create an array with a non-const value for the size, like you're trying to do in your code.
2) In your code, you've created two variables called x, with different scope, one of which hides the other. Does that strike you as a sensible thing to do?
3) As you've guessed, you're better off using a vector. If you use a vector of pointers to MY_WIDGET, then, again, the MY_WIDGET constructor is not invoked when you push pointers onto the vector.
If, however, you want to use a vector of actual MY_WIDGET objects, then it's the
copy constructor that gets invoked when you push them onto the vector. It's likely that you'll need to write one of those, if you haven't already, unless you're happy with the default one.