开发者

Initializing an array of objects

I'm currently working on a card game, and I'm having trouble with some initialization code:

// in my class...
Card cards[20];
// in method...
for(int i = 0; i <= 20;i++)
    cards++ = new Card(i, /*i as char +*/ "_Card.bmp");

The trouble is that my compiler's telling me that cards++ is not an l-value. I've read up on the whole pointer-array equivalence thing, and I thought I understood it, but alas, I can't get it to work. My understanding is that since cards degrades to a pointer, and the new operator gives me a pointer to the location of my new instance of Card, then the above code should compile. Right?

I've tried using a subscript as well, but isn't cards+i, cards++, 开发者_JAVA技巧and cards[i] just 3 ways of saying the same thing? I thought that each of those were l-values and are treated as pointers.


Card cards[20];

cards is already an array of objects. They are constructed with the default constructor(constructor with no arguments). There is no need to new again. Probably you need a member function equivalent to constructor arguments and assign through it.

for ( int i=0; i<20; ++i ) // array index shouldn't include 20
   cards[i].memberFunction(/*....*/);

Even simpler is to use std::vector

std::vector<Card> cards;
for( int i=0; i<20; ++i )
    cards.push_back(Card(i, /*i as char +*/ "_Card.bmp"); )


The code Card cards[20]; already creates an array of 20 Card objects and creates them with the default constructor. This may not be what you want given your code.

I would suggest using vector instead.

std::vector<Card> cards;

for(int i = 0; i < 20;i++)
{
    cards.push_back(Card(i, /*i as char +*/ "_Card.bmp"));
}

Note that your for loop goes from 0 to 20 and thus one past the end of the array.


If you want to avoid unnecessary constructor calls and unnecessary resizing, then it's more complicated, because C++ normally initialises each objects one-by-one as it's allocated. One workaround is to do it the Java way -- use a loop and an array of pointers, like so:

Card *cards[20];
for (int i=0; i<20; i++) {
  cards[i] = new Card(i);
}

Another option is to use malloc to get explicitly uninitialized memory:

Card *cards = malloc(20 * sizeof(Card));
for (int i=0; i<20; i++) {
  new (&(cards[i])) Card(i);
}


Well, there is another possibility, when you are ok with your constructors being called automatically at initialization:

// in my class...
Card cards[20] = { Card(0, "0_Card.bmp"), Card(1, "1_Card.bmp"), /* ... */ };

The huge downside is that you cannot use a loop in this case.


An array name, cards in your code, contains the address of the first element of the array. Such addresses are allocated at run time and you cannot change them. Hence the compiler complaining about cards being not an l-value.

But you can definitely specify what those addresses can hold by using a pointer like below:

// in my class...
Card cards[20];

Card *cardsPointer = cards;// Pointer contains the address of the
//1st element of 'cards' array.

// in method...
for(int i = 0; i < 20; i++)
*(cardsPointer++) = Card(i, /*i as char +*/ "_Card.bmp");// Note that 
// there is no 'new' operator as 'cardsPointer' has type 'Card *' and 
// not 'Card **'. And 'cardsPointer' has type 'Card *' as the array is
// of type 'Card'.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜