operator overloading

Started by
11 comments, last by password 17 years, 4 months ago
That's not a two-dimensional vector. It's a vector of vectors. Honestly, if your "2D array" is rectangular, you shouldn't use a vector of vectors. It's clunky. Instead, use a single vector and construct it like:
Block::Block(int width,int height): myVector(width * height);{  // ...}


Or something like that. Then you can remove the implicit conversion to vector-of-vectors and replace it with an overloaded operator() (or a getPart() method, or something) that looks like:

int Block::getPart(int x,int y) // int Block::operator()(int x,int y){  return (myVector[x + width * y]);}


It will save you a lot of headaches in the long run.
Advertisement
Quote:Original post by jpetrie
That's not a two-dimensional vector. It's a vector of vectors. Honestly, if your "2D array" is rectangular, you shouldn't use a vector of vectors. It's clunky. Instead, use a single vector and construct it like:
Block::Block(int width,int height): myVector(width * height);{  // ...}

Or even better: welcome to the golden city of Boost.MultiArray.
Last time I tested it only made things complicated. Well, I can give it another chance and use a single vector instead. Thanks for your help.

If i'm going to show the block on the screen, how would I do it the best way? If I were to do it, I would do it like this.

int ypos=0;        for (int i=0;i<(4*4);i++) {            if (Data!=0) BlitSurface(125+(i*BLOCKSIZE), 125+ypos, blocks, &blocktypes[1]);            if ((i+1)%4==0) ypos+=BLOCKSIZE;        }


I'm sure you can do it with a formula but i'm not sure how.

This topic is closed to new replies.

Advertisement