gpt4 book ai didi

c++ - 如何从 getter 函数中的类返回结构数组

转载 作者:行者123 更新时间:2023-11-30 04:20:49 25 4
gpt4 key购买 nike

我有一个相对简单的问题,但我似乎找不到适合我的情况的答案,而且我可能没有以正确的方式处理这个问题。我有一个看起来像这样的类:

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();
};

对于 getter 函数,我有一个看起来像这样的实现:

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

我在 LoadMap 类中有代码将为数组中的每个结构分配 100 个图 block 属性。我希望能够在我的 main.cpp 文件中访问这个结构数组,但我似乎无法找到正确的语法或方法。我的 main.cpp 看起来像这样。

 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;
}

我知道有很多方法可以解决这个问题,但我正在努力使这个实现尽可能保密。如果有人能指出我正确的方向,我将不胜感激。谢谢!

最佳答案

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

这将为您提供指向返回数组中第一个元素 的指针。然后您可以遍历其他 99 个。

我强烈建议使用 vector 或其他容器,并编写不会像那样返回指向裸数组的指针的 getter。

关于c++ - 如何从 getter 函数中的类返回结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14995216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com