Passing array of pointers to another class

Posted by user310153 on Stack Overflow See other posts from Stack Overflow or by user310153
Published on 2010-04-06T15:18:23Z Indexed on 2010/04/06 15:23 UTC
Read the original article Hit count: 166

Filed under:
|
|

Hi, I am trying to do the following:

in main.cpp:

// Create an array of pointers to Block objects
Block *blk[64];
for (i=0; i<8; i++) {
  for (j=0; j<8; j++) {
    int x_low = i*80;
    int y_low = j*45;
    blk[j*8+i] = new Block(30, x_low+40.0f, y_low+7.5f, &b);
  }
}

And then I am trying to pass it to the graphics object I have created:

Graphics g(640, 480, &b, &p, blk[0], number_of_blocks);

the graphics constructor looks like:

Graphics::Graphics(int width, int height, Ball *b, Paddle *p, Block *blk, int number_of_blocks) {

if I look at what is contained in the array from the graphics object, only the first item exists and then all the other items are in hyperspace:

for (int i=0; i<64; i++) {
  printf("for block %d, %f, %f ", i, (_blk+(sizeof(_blk)*i))->_x_low, (_blk+(sizeof(_blk)*i))->_y_low);

  printf("blah %d\n", (_blk+(sizeof(_blk)*i)));

}

and if I look at the addresses, they are different (6956552 rather than 2280520 when I examine the addresses in the main class using:

printf(" blah %d\n", &blk[j*8*i]);

I am sure there must be something subtle I am doing wrong as its like I have copied the first item from the blk array to a new address when passed to the graphics object.

Does this make sense? Any ideas?

Cheers, Scott

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers