1

I have a relatively simple question but I cant seem to find an answer specific for my case and I just may not be approaching this problem the right way. I have a class that looks like this:

struct tileProperties
{
    int x;
    int y;
};

class LoadMap
{      
  private:        
    ALLEGRO_BITMAP *mapToLoad[10][10];   
    tileProperties *individualMapTile[100]; 

  public: 
    //Get the struct of tile properties
    tileProperties *getMapTiles();
};

I have an implementation that looks like this for the getter function:

tileProperties *LoadMap::getMapTiles()
{
    return individualMapTile[0];
}

I have code in the LoadMap class that will assign 100 tile properties for each struct in the array. I want to be able to access this array of structs in my main.cpp file but I just cant seem to find the right syntax or approach. My main.cpp looks like this.

 struct TestStruct
{
    int x;
    int y;
};

int main()
{
   LoadMap  _loadMap;
   TestStruct *_testStruct[100];
    //This assignment will not work, is there
    //a better way?
   _testStruct = _loadMap.getMapTiles();

   return 0;
}

I realize that there are many approaches to this, but I'm trying to keep this implementation as private as possible. If someone could please point me in the right direction I would greatly appreciate it. Thank you!

Aesthete
  • 18,622
  • 6
  • 36
  • 45
zic10
  • 2,310
  • 5
  • 30
  • 55
  • possible duplicate of [return an array from function](http://stackoverflow.com/questions/6422993/return-an-array-from-function) – jogojapan Feb 21 '13 at 05:33

3 Answers3

2
TestStruct *_testStruct;
_testStruct = _loadMap.getMapTiles();

This will get you a pointer to the first element in the array returned. You can then iterate through the other 99.

I would highly recommend using vectors, or another container, and writing getters that don't return pointers to bare arrays like that.

Aesthete
  • 18,622
  • 6
  • 36
  • 45
0

First of all, here, why do we need TestStruct, you can use "tileProperties" structure itself...

And imp thing, tileProperties *individualMapTile[100]; is array of pointers to the structure.

Hence, individualMapTile will have pointers in it. You have returned the first pointer, hence you can access the first structure only. What about the others????

tileProperties** LoadMap::getMapTiles()
{
  return individualMapTile;
}

int main()
{
   LoadMap _loadMap;
   tileProperties **_tileProperties;
  _tileProperties = _loadMap.getMapTiles();

    for (int i=0; i<100;i++)
{
    printf("\n%d", (**_tileProperties).x);
    _tileProperties;
}
   return 0;
}
51k
  • 1,381
  • 3
  • 12
  • 22
0

Use vectors instead of arrays where possible. Also consider an array/vector of TestStruct directly rather than pointers to them. I can't tell if that would be appropriate for you from your code sample.

class LoadMap
{      
public:
    typedef vector<tileProperties *> MapTileContainer;

    LoadMap()
        : individualMapTile(100) // size 100
    {
        // populate vector..
    }

    //Get the struct of tile properties
    const MapTileContainer& getMapTiles() const
    {
        return individualMapTile;
    }

    MapTileContainer& getMapTiles()
    {
        return individualMapTile;
    }

private:         
    MapTileContainer individualMapTile; 
};

int main()
{
    LoadMap _loadMap;
    LoadMap::MapTileContainer& _testStruct = _loadMap.getMapTiles();
}
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91