Array or Vector of Widgets?

I am trying to create an array of objects. I have to be abstract unfortunately (can't show you all my code), but this is the general gist of what I'm doing. Let's say these objects will be Widgets.

I would assume a vector of widgets would be better than an array due to it's flexibility. So if I use an array, let's say that my widjet is called MY_WIDGET and MY_WIDGET has a constructor of MY_WIDGET(int x, int y);

I have tried using an array to hold several widgets like this.

1
2
3
4
5
6
7
8
9
10
11
int i = 10; // obviously this number will be decided dynamically.
int x = 10;
int y = 10;

MY_WIDGET *array[i];

for(int x = 0; x < i; x++)
{
arr[x] = new MY_WIDGET(x,y);
y +=30; // moves the next widget down so they don't cover each other.
}


So now I have an array of objects or Widgets in this case. I'm only using widgets for this example because I want to display the use of a constructor.

So, while my compiler (MinGW) will let me do this, I still get an error at runtime because of the way I have dynamically sized my array 'arr[i]'.

The usual way of doing this, let's say for an int (see below), I can't apply for an object with a constructor requiring multiple parameters to construct it. Maybe I can't, I'm not sure.

1
2
int *x = 0;
x = new int[10];


This begs the question, should I be using a vector and if so, how do I dynamically push_back constructed objects into a vector without running into the same problem above. I have tried the below and get problems at compile time telling me that 'the base class is private within this context'. Not sure how to go about this but if I can understand how to dynamically create objects with constructors and add them to a vector or array I should be able to do this. Would anyone be able to help? Thanks.


1
2
3
4
5
6
vector<MY_WIDGET> myvec;

MY_WIDGET the_widget(x,y);
myvec.push_back(the_widget);

y+=30;  



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.
Last edited on
If you are using C++11, you can use emplace_back() vector member. It constructs elements in place without need to invoke copy constructor:

1
2
3
4
5
6
7
8
9
class foo
{
  public:
    foo(int, std::string){}
}

//...
std::vector<foo> v;
v.emplace_back(0, "Hello");
Thanks allot for that. I'm not using C++ 11 yet but that will be useful when I do.

MikeyBoy, that works perfectly. I've never known how to do this before.

Can I ask for an interpretation of this line.

I think it means that I am allocating memory space for 'numWidgets' amount of My_WIDGET objects. Is that correct?

The syntax new MY_WIDGET*[numWidgets] is a little unusual to me but it works perfectly. Is it like mallocing space for a number of MY_WIDGET objects?

MY_WIDGET** arr = new MY_WIDGET*[numWidgets];

I think it means that I am allocating memory space for 'numWidgets' amount of My_WIDGET objects. Is that correct?


No. It's allocating space for 'numWidgets' amount of pointers to MY_WIDGET objects. It doesn't allocate any space for the objects themselves.

new is the C++ way of dynamically allocating memory. You used it yourself in the code in your original post, so I assumed you knew what it is.
Last edited on
Topic archived. No new replies allowed.