Pointer Matrix

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    Pointer Matrix

    hi, I need some help declaring a matrix of pointers. I made an image
    to illustrate what I had in mind...



    and here's the relevant code I'm working with (doesn't work)

    class Tile
    {
    public:
    SDL_Surface *img;
    bool (*col)();
    Tile(char* filename) {
    img = IMG_Load(filena me);
    }
    };

    Tile square("img/square.png");
    Tile triangle("img/triangle.png");

    class Map
    {
    Tile ***tile;
    Map()
    {
    tile = new *Tile[16][16];

    I can't use tile = new Tile[16][16]; because then it actually tries to
    construct each of the tiles, when I just want a matrix of pointers,
    and I will assign each tile later individually... . like so

    for(int y=0; y<16; y++)
    {
    for(int x=0; x<16; x++)
    {
    switch( rand()%2 )
    {
    case 0:
    tile[x][y] = square;
    break;
    case 1:
    tile[x][y] = triangle;
    break;
    }
    }
    }
    }

    I can't seem to figure out the proper syntax...

    also..how would I properly delete this?
    delete [][]tile?

  • Mark

    #2
    Re: Pointer Matrix

    On Mar 9, 6:19 pm, "Mark" <mnbaya...@gmai l.comwrote:
    hi, I need some help declaring a matrix of pointers. I made an image
    to illustrate what I had in mind...
    >

    >
    and here's the relevant code I'm working with (doesn't work)
    >
    class Tile
    {
    public:
    SDL_Surface *img;
    bool (*col)();
    Tile(char* filename) {
    img = IMG_Load(filena me);
    }
    >
    };
    >
    Tile square("img/square.png");
    Tile triangle("img/triangle.png");
    >
    class Map
    {
    Tile ***tile;
    Map()
    {
    tile = new *Tile[16][16];
    >
    I can't use tile = new Tile[16][16]; because then it actually tries to
    construct each of the tiles, when I just want a matrix of pointers,
    and I will assign each tile later individually... . like so
    >
    for(int y=0; y<16; y++)
    {
    for(int x=0; x<16; x++)
    {
    switch( rand()%2 )
    {
    case 0:
    tile[x][y] = square;
    break;
    case 1:
    tile[x][y] = triangle;
    break;
    }
    }
    }
    }
    >
    I can't seem to figure out the proper syntax...
    >
    also..how would I properly delete this?
    delete [][]tile?
    the code below makes more sense, but still doesn't compile

    Tile (*tile)[16][16];

    public:
    Map()
    {
    for(int y=0; y<16; y++)
    {
    for(int x=0; x<16; x++)
    {
    switch( rand()%2 )
    {
    case 0:
    tile[x][y] = &square;
    break;
    case 1:
    tile[x][y] = &triangle;
    break;
    }
    }
    }
    }

    Comment

    • John Harrison

      #3
      Re: Pointer Matrix

      Mark wrote:
      hi, I need some help declaring a matrix of pointers. I made an image
      to illustrate what I had in mind...
      >

      >
      and here's the relevant code I'm working with (doesn't work)
      >
      class Tile
      {
      public:
      SDL_Surface *img;
      bool (*col)();
      Tile(char* filename) {
      img = IMG_Load(filena me);
      }
      };
      >
      Tile square("img/square.png");
      Tile triangle("img/triangle.png");
      >
      class Map
      {
      Tile ***tile;
      Map()
      {
      tile = new *Tile[16][16];
      >
      I can't use tile = new Tile[16][16]; because then it actually tries to
      construct each of the tiles, when I just want a matrix of pointers,
      and I will assign each tile later individually... . like so
      >
      for(int y=0; y<16; y++)
      {
      for(int x=0; x<16; x++)
      {
      switch( rand()%2 )
      {
      case 0:
      tile[x][y] = square;
      break;
      case 1:
      tile[x][y] = triangle;
      break;
      }
      }
      }
      }
      >
      I can't seem to figure out the proper syntax...
      >
      also..how would I properly delete this?
      delete [][]tile?
      >
      Why are you allocating this matrix? It's 16x16 always so there is no
      need to allocate.

      class Map
      {
      Tile* tile[16][16];
      };

      If you really did want to allocat then it would be

      class Map
      {
      Tile* (*tile)[16];
      Map()
      {
      tile = new Tile*[16][16];
      }
      };

      This only works because the first dimension of your array is constant.

      You delete the same way you delete any array allocation

      delete[] tile;

      john

      Comment

      • Mark

        #4
        Re: Pointer Matrix

        On Mar 9, 11:57 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
        Mark wrote:
        hi, I need some help declaring a matrix of pointers. I made an image
        to illustrate what I had in mind...
        >>
        and here's the relevant code I'm working with (doesn't work)
        >
        class Tile
        {
        public:
        SDL_Surface *img;
        bool (*col)();
        Tile(char* filename) {
        img = IMG_Load(filena me);
        }
        };
        >
        Tile square("img/square.png");
        Tile triangle("img/triangle.png");
        >
        class Map
        {
        Tile ***tile;
        Map()
        {
        tile = new *Tile[16][16];
        >
        I can't use tile = new Tile[16][16]; because then it actually tries to
        construct each of the tiles, when I just want a matrix of pointers,
        and I will assign each tile later individually... . like so
        >
        for(int y=0; y<16; y++)
        {
        for(int x=0; x<16; x++)
        {
        switch( rand()%2 )
        {
        case 0:
        tile[x][y] = square;
        break;
        case 1:
        tile[x][y] = triangle;
        break;
        }
        }
        }
        }
        >
        I can't seem to figure out the proper syntax...
        >
        also..how would I properly delete this?
        delete [][]tile?
        >
        Why are you allocating this matrix? It's 16x16 always so there is no
        need to allocate.
        >
        class Map
        {
        Tile* tile[16][16];
        >
        };
        >
        If you really did want to allocat then it would be
        >
        class Map
        {
        Tile* (*tile)[16];
        Map()
        {
        tile = new Tile*[16][16];
        }
        >
        };
        >
        This only works because the first dimension of your array is constant.
        >
        You delete the same way you delete any array allocation
        >
        delete[] tile;
        >
        john
        erm..i discovered a solution, hence the deleted posts. i wasn't sure
        if it was necessary to allocate memory for them or not, and ideally i
        don't want a constant 16x16 grid, that was just for sample. decided
        to use a <vector<vector< Tile*
        but thank you for the help :)

        Comment

        • Default User

          #5
          Re: Pointer Matrix

          Mark wrote:

          erm..i discovered a solution, hence the deleted posts.
          For future reference, that doesn't really do much except remove them
          from the Google archives. Most of the participants here are not reading
          via GG, and your deletion doesn't necessarily affect the copies on
          other servers. I'm not entirely sure if that sends a cancel message or
          not.

          In general, it's best not to try to delete them, as you get an
          indeterminant state where some people see the referenced message and
          others don't.





          Brian

          Comment

          Working...