constructors and dynamic arrays

I have a 2 classes, class A and class B. Class A contains a pointer to a dynamically allocated array of class B's, created in Class A's constructor. I need to pass arguments to Class Bs' constructors when they are allocated. Is there any way to do this, or will I have to call some kind of Init() function? Hopefully this makes sense to someone.
When you allocate an array of dynamic memory, default constructors will be called. There is no way to make it call any other kind of constructor. I suppose Init would be the best option. Alternatively you could malloc and then placement-new, but that might be a mess.
Or use std::vector (reserve and push_back), so you don't have to worry about handling the memory.
A useful trick is to write all of B's constructor code in Init() and then make the constructor simply call Init(). That way, should you make a change to the code later on, you only have to do it once.
Thanks for the help, everyone. Vectors seems like the way to go, but I'll still move the constructor stuff into an Init() function for my classes.
You can create a vector filled with copies of a particular element:
vector<Foo> vec(n,Foo(42));

Whether that makes sense depends on the situation.
Topic archived. No new replies allowed.