simple inheritance question

Discussion in 'C++' started by Numeron, Nov 27, 2007.

  1. Numeron

    Numeron Guest

    This is part of my first ever largish project, a tile based game. I
    havent got a lot of experience with c++ so be nice ;)

    this is all within Tile.h, a lot has been taken out or simplified for
    the sake of...er simplicity lol.

    class Tile{
    private:
    int locationX;
    int locationY;
    public:
    Tile(int inLocX, int inLocY){
    setLocX(inLocX);
    setLocY(inLocY); }
    void setLocX(int inLocX){ locationX = inLocX; }
    void setLocY(int inLocY){ locationY = inLocY; }
    int getLocX(){ return locationX; }
    int getLocY(){ return locationY; }
    }//end of Tile class

    class FloorTile : public Tile{
    private:
    int roomID;
    public;
    FloorTile(int inLocX, int inLocY, int inRoomID):Tile(inLocX, inLocY)
    {
    setRoomID(roomID); }
    void setRoomID(int inRoomID){ roomID = inRoomID; }
    int getRoomID(){ return roomID; }
    }//end of FloorTile class

    the idea is that i have a 2d array called map which holds tiles of
    various kinds inlcuding wall, void and as seen above, floor. All of
    that works fine, but when i look up, for example the tile at position
    10,10 and i *know* that it is a floor tile, calling setRoomID() doenst
    work. Origional code looks somthing like this:
    map[10][10].setRoomID(15);
    ive been shotgun programming it all afternoon, the best idea ive had
    is pretty ugly, but still wont work because FloorTile is non-scalar
    and cant be cast like thus:
    FloorTile tempTile = map[10][10];
    tempTile.setRoomID(15);
    I guess what i need is some way to get it recognising the difference
    between the types of tiles, once theyre pulled out of the array,
    thereby opening the possibility of accessing the methods of the
    subclass, but i have no idea...

    sorry for noobing it up, im pretty sure this is a stupid thing to get
    stuck on :p

    -Numeron
     
    Numeron, Nov 27, 2007
    #1
    1. Advertising

  2. Numeron

    Zeppe Guest

    Numeron wrote:
    > This is part of my first ever largish project, a tile based game. I
    > havent got a lot of experience with c++ so be nice ;)
    >
    > this is all within Tile.h, a lot has been taken out or simplified for
    > the sake of...er simplicity lol.
    >
    > class Tile{
    > private:
    > int locationX;
    > int locationY;
    > public:
    > Tile(int inLocX, int inLocY){
    > setLocX(inLocX);
    > setLocY(inLocY); }
    > void setLocX(int inLocX){ locationX = inLocX; }
    > void setLocY(int inLocY){ locationY = inLocY; }
    > int getLocX(){ return locationX; }
    > int getLocY(){ return locationY; }
    > }//end of Tile class
    >
    > class FloorTile : public Tile{
    > private:
    > int roomID;
    > public;
    > FloorTile(int inLocX, int inLocY, int inRoomID):Tile(inLocX, inLocY)
    > {
    > setRoomID(roomID); }
    > void setRoomID(int inRoomID){ roomID = inRoomID; }
    > int getRoomID(){ return roomID; }
    > }//end of FloorTile class
    >
    > the idea is that i have a 2d array called map which holds tiles of
    > various kinds inlcuding wall, void and as seen above, floor. All of
    > that works fine, but when i look up, for example the tile at position
    > 10,10 and i *know* that it is a floor tile, calling setRoomID() doenst
    > work.


    How can you know it? I'm pretty curious to know how you declare your 2d
    vector. If it is a Tile[][], all your tiles are basic tiles. The
    (design) problem anyway is not so trivial, even though it's quite common
    and there is a choice of solutions depending on the particular situation.

    Regards,

    Zeppe
     
    Zeppe, Nov 27, 2007
    #2
    1. Advertising

  3. Numeron

    Numeron Guest


    > How can you know it? I'm pretty curious to know how you declare your 2d
    > vector. If it is a Tile[][], all your tiles are basic tiles. The
    > (design) problem anyway is not so trivial, even though it's quite common
    > and there is a choice of solutions depending on the particular situation.


    declaring looks something like this:

    Tile map[78][39];

    void InitializeMap(){
    //Iterates through the map filling it with floor tiles
    int x = 0; int y = 0;
    while ((x != 78) && (y != 39)){
    map[x][y] = new FloorTile(x,y,0);
    x++;
    if (x == 78){ y++; x = 0; }
    }
    }

    once the initialise map method is called they're *all* floor tiles, so
    any tile pulled from the map array must also be a floor tile..

    -Numeron
     
    Numeron, Nov 27, 2007
    #3
  4. Numeron

    Zeppe Guest

    Numeron wrote:
    >> How can you know it? I'm pretty curious to know how you declare your 2d
    >> vector. If it is a Tile[][], all your tiles are basic tiles. The
    >> (design) problem anyway is not so trivial, even though it's quite common
    >> and there is a choice of solutions depending on the particular situation.

    >
    > declaring looks something like this:
    >
    > Tile map[78][39];
    >
    > void InitializeMap(){
    > //Iterates through the map filling it with floor tiles
    > int x = 0; int y = 0;
    > while ((x != 78) && (y != 39)){
    > map[x][y] = new FloorTile(x,y,0);


    This won't compile. Something like "invalid conversion from
    'FloorTile*"' to 'Tile'", I guess. Anyway, assuming you didn't wat to
    use the new, I'm afraid in the map all the Tiles are just simple Tiles,
    and the FloorTile that you assign in the floor is converted implicitly
    into a Tile object. If you want polymorphism, you have to use pointers:

    Tile* map[78][39];

    for(...)
    for(...)
    map[j] = new [whatever you want]

    and then, when you want to access the FloorTile members, you can do a
    static_cast<FloorTile*>(map[j])->floorTileMethod();
    of course, this is going to terminate badly your program if map[j] is
    not a FloorTile. In order to prevent so, you can test the type with a
    dynamic_cast, having the type enumerated in the base class, or any other
    possibly fancier option.

    Regards,

    Zeppe
     
    Zeppe, Nov 27, 2007
    #4
  5. On Tue, 27 Nov 2007 07:48:27 -0800, Numeron wrote:
    [FloorTile is a subclass of Tile - introduces new method]
    >> How can you know it? I'm pretty curious to know how you declare your 2d
    >> vector. If it is a Tile[][], all your tiles are basic tiles. The
    >> (design) problem anyway is not so trivial, even though it's quite
    >> common and there is a choice of solutions depending on the particular
    >> situation.

    >
    > declaring looks something like this:
    >
    > Tile map[78][39];
    >
    > void InitializeMap(){
    > //Iterates through the map filling it with floor tiles int x = 0;
    > int y = 0;
    > while ((x != 78) && (y != 39)){
    > map[x][y] = new FloorTile(x,y,0);
    > x++;
    > if (x == 78){ y++; x = 0; }
    > }
    > }
    >
    > once the initialise map method is called they're *all* floor tiles, so
    > any tile pulled from the map array must also be a floor tile..


    First of all - your code doesn't compile - the line
    map[x][y] = new FloorTile(x, y, 0);
    The left side of assignment has type Tile, while the right side is of
    type FloorTile*. Maybe your array has type
    Tile *map[78][39];

    Second - if the whole array contains objects of FloorTile, why bother
    with declaring it as containing objects of class Tile?
    --
    Tadeusz B. Kopec ()
    "A dirty mind is a joy forever."
    -- Randy Kunkee
     
    Tadeusz B. Kopec, Nov 27, 2007
    #5
  6. Numeron

    Numeron Guest

    Zeppe wrote:
    >Tile* map[78][39];
    >
    >
    >for(...)
    > for(...)
    > map[j] = new [whatever you want]
    >
    >
    >and then, when you want to access the FloorTile members, you can do a
    >static_cast<FloorTile*>(map[j])->floorTileMethod();


    thanks for this it will be immesurably helpful. i understand how
    pointers work, but in practice its a bit tricky to get right, but hey,
    i guess i need to learn sometime ;)

    Tadeusz B. Kopec wrote:
    > Second - if the whole array contains objects of FloorTile, why bother
    > with declaring it as containing objects of class Tile?


    basically thats jsut the first part of the level generating
    algorithm.. fills with floor, puts walls in tiles to give rectangled
    rooms, adds some other stuff etc. so that the whole thing turns out to
    look something like an old 'roguelike' game.

    -Numeron
     
    Numeron, Nov 28, 2007
    #6
  7. On Tue, 27 Nov 2007 18:02:49 +0000, Zeppe wrote:

    > Numeron wrote:
    >>> How can you know it? I'm pretty curious to know how you declare your
    >>> 2d vector. If it is a Tile[][], all your tiles are basic tiles. The
    >>> (design) problem anyway is not so trivial, even though it's quite
    >>> common and there is a choice of solutions depending on the particular
    >>> situation.

    >>
    >> declaring looks something like this:
    >>
    >> Tile map[78][39];
    >>
    >> void InitializeMap(){
    >> //Iterates through the map filling it with floor tiles int x = 0;
    >> int y = 0;
    >> while ((x != 78) && (y != 39)){
    >> map[x][y] = new FloorTile(x,y,0);

    >
    > This won't compile. Something like "invalid conversion from
    > 'FloorTile*"' to 'Tile'", I guess. Anyway, assuming you didn't wat to
    > use the new, I'm afraid in the map all the Tiles are just simple Tiles,
    > and the FloorTile that you assign in the floor is converted implicitly
    > into a Tile object. If you want polymorphism, you have to use pointers:
    >
    > Tile* map[78][39];
    >
    > for(...)
    > for(...)
    > map[j] = new [whatever you want]
    >
    > and then, when you want to access the FloorTile members, you can do a
    > static_cast<FloorTile*>(map[j])->floorTileMethod(); of course, this
    > is going to terminate badly your program if map[j] is not a
    > FloorTile. In order to prevent so, you can test the type with a
    > dynamic_cast, having the type enumerated in the base class, or any other
    > possibly fancier option.


    It's good to note that using static_casts and dynamic_casts often
    indicates poor design. I bet this is the case.
    I also noticed that Tile has no virtual destructor although it is used
    polymorphically. On deletion there will be undefined behaviour.
    --
    Tadeusz B. Kopec ()
    My opinions may have changed, but not the fact that I am right.
     
    Tadeusz B. Kopec, Nov 28, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,179
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    842
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,061
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,942
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    788
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page