So let's say I have defined the following data types:
struct Point { int x; int y; Point(int ex, int ey) { x = ex; y = ey; } } struct Rectangle { Point p; int w; int h; Rectangle(Point ep, int ew, int eh) { p = ep; w = ew; h = eh; } }
Now I want to create a bunch of Rectangles using a for loop, and place them in an array, like so.
Rectangle * rect = new Rectangle[10]; for (int x=0; x<10; x++) { Rectangle newRect(Point(0,x*10),10,20); *rect[x] = newRect; }Problem is, the C++ compiler complains on the first line of the above code about not having a default constructor for Rectangle. I can get around this using some absolutely disgusting tricks with void pointers and reinterpret_cast (although it seems to me I shouldn't have to do this), but where I really get stuck is in trying to assign to *rect[x]. When I do this, the compiler complains about not being able to access various private functions of Rectangle, and some error related to "operator ="
That's because in reality, Rectangle and Point are part of a library (and are actually much more sophisticated structures than I wrote above), and modifying those structures is not an option in this case.
Ideally, I'd like to be able to avoid this pointer business altogether and just use a std::vector<Rectangle> instead, but I'll settle for anything that allows me to create and iterate through a collection of Rectangles.
Much appreciated!