Re: object arrays and constuctors
On Mar 20, 6:37 pm, "tripleF" <s...@night.co.uk> wrote:
I want to pass parameters to the consructors when i create an array of
objects like this for a single object
Object *pSprite;
pSprite=new Object(1,1);
dynamicly created object array
Object *pSprite;
pSprite=new Object[5];
How do i combine the two?
If all objects in the array should have the same initializer:
std::vector< Object > v( 5, Object( 1, 1 ) ) ;
otherwise:
std::vector< Object > v( iter1, iter2 ) ;
You'll need some fancy iterators. I'd design Object so that it
only needed a single argument, an std::pair, or a boost::tuple,
if that's what it takes. The iterator returns this argument.
Alternatively, the iterator returns an Object, which is copied.
I'd seriously look into the Boost iterator adapters if I had to
do something like this. I believe they have a counting iterator
adapter, for example.
Note, however, that the usual idiom for this is something along
the lines of:
std::vector< Object > v ;
for ( int i = 0 ; i < 5 ; ++ i ) {
v.push_back( Object( ... ) ) ;
}
Depending on the source of your initializers, this might be just
as easy and as clear.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]