Initializing array of objects in an object?

Hi All,

A very basic question:

The question is embedded inside the code in class B.

Thanks
K

class A
{

int width;
int *y;
public:
A::A(int w)
{
width = w;
y = new (int[w]);
};

};


class B
{
int width;
A *p;

public:
B::B(int w)
{
width = w;
// How do i initialize p with an array of "w" objects of type A and also pass the initializer argument w to each of the object A in the array?

};

};


Last edited on
There's a couple ways of doing this the way you are asking, I wouldn't call any of them "great".

The first thing that comes to my mind is to use a for loop to create new 'A' objects with "w" as the argument to the constructor. This method has almost zero bounds checking but as long as you aren't going nuts it could work.
ok. will try it. And whats the second way?
I was toying with the idea of overloading your 'A' class constructor to allow declaration of the object and leave assigning values up to a member function later on. But there are too many things that could go wrong with that.

Is this purley academic? I'd be interested in knowing what the end game for something like this would be.
Topic archived. No new replies allowed.