# Another question :O need ideas and confirmation

This is a discussion on Another question :O need ideas and confirmation within the C++ Programming forums, part of the General Programming Boards category; I know I'm asking loads of questions, but when I get frusterated I turn to you peeps Anyways, I'm trying ...

1. ## Another question :O need ideas and confirmation

I know I'm asking loads of questions, but when I get frusterated I turn to you peeps

Anyways, I'm trying to make a tile based game, and now I have this struct

Code:
```struct tileSlot{
object obj;
float x, y;

};```
And an array
Code:
`tileSlot slots[31][23];`
And then I set a fixed position to all the 'slots', using this code

Code:
```void tileSlotPos(){
float width = 0;
//width loop
for(int w = 0; w >= 31; ++w){
float height = 0;
//height loop
for(int h = 0; h >= 23; ++h){
slots[w][h].x = width;
slots[w][h].y = height;

height += 32;
}
width += 32;
}

}```
and then I create the platforms using this code.

Code:
```void createPlat(int slotW, int slotH){
platform plat;
slots[slotW][slotH].obj = plat;
plat.x = slots[slotW][slotH].x;
plat.y = slots[slotW][slotH].y;

plats.push_back(plat);

}```
Then later when I call, for testing.

Code:
```tileSlotPos();

createPlat(20, 20);```
the platform ends up in the top left corner, meaning x= 0 and y = 0, or slot[0][0]
Do you peeps have any idea why it does this? And do you have any idea for a different approach to doing this?

2. Eh, it looks like your for loop will not run at all, since w >= 31 is false as the first iteration begins.

3. Your tile contains an object instance. You create a platform instance on the stack, assign it to that object instance, and then modify the platform instance. You expect the object instance to have the values you gave the platform instance. This is wrong for four reasons:

1) The assignment copies the values. There remains no connection between the object instance in the tile instance and the platform instance on the stack after the copying is done.
2) The assignment slices. Basically, only the object subpart of the platform instance is copied, everything else is ignored. This is not good at all.
3) If the assignment actually copied everything, you'd be overwriting memory, because a platform instance is larger than an object instance. And you still wouldn't retain the values that you assigned to the platform instance after you copied from it.
4) If the assignment somehow made the object instance reference the platform instance (like this code would do in Java), you'd have a dangling reference, because the platform instance is destroyed when createPlat returns.

The correct solution depends on the circumstances, but probably consists of making the object instance in tile a smart pointer to object, and allocating a platform instance on the heap in createPlat (with new), making the smart pointer take control of the instance.

On a side note, is it really so much work to write createPlatform instead of createPlat?

4. well, platform is a class inherited from object
Code:
`class platform : public object`
Is it still wrong?
And, laserlight, thanks ^^ I hate such errors xP
EDIT: it works now

5. I expected it to be inheriting (not inherited, that means something else), but it's still wrong. You say it works, I say it contains a bug that will bite you very seriously indeed.

6. why o.o?

And I already ended with a bug, the bug is when I set the height of the slot array, to 23, like slots[0][23], the platform jumps up to, slots[1][0], now why does it do that? Is it that bug you talked about, or something completely different?

7. Look up the term "slicing" as it pertains to C++ programming.

As for the other, there is no slots[0][23]. The highest index of the second dimension is 22. 23 is an out of bounds access, leading to undefined behavior. (The memory layout of arrays dictates that, in effect, accessing slots[0][23] is equivalent to accessing slots[1][0], but that's just happenstance.)

8. ah dammit, slicing sounds bad >.<
Why can't it just be simple!? D: well, I'm too lazy to fix it right now xP