成员数组包含类的实例。无法决定在哪里放置修改此类变量的方法

发布于 2024-12-12 09:00:26 字数 1265 浏览 1 评论 0原文

Map 类中有一个由 Block 组成的二维数组。我需要一个方法来更改类 Block 中的变量 value

class Block
{
    Block(BlockValue blockValue = BLANK);

    //first solution
    void Block::setValue(BlockValue blockValue, Map &map);

    void setImage(BlockValue blockValue, int n = 0);

    //the value to be changed
    BlockValue value;

    static std::vector< std::vector<sf::Image> > images;

    ...
};

 class Map
 {
     std::vector< std::vector<Block> > blocks;

     //second solution
     void setBlockValue(BlockValue blockValue, int i, int j);

     ...
 };

我可以想到两种解决方案:

  1. 我在类 Block 中创建一个 Block::setValue(BlockValue blockValue, Map &map) 并从 Block 中调用它code>Map 的方式如下:

    blocks[i][j].setValue(blockValue, this);

  2. 我创建了一个setBlockValue(BlockValue blockValue, int i, int j) 方法 int 类 Map。此解决方案的问题是我无法将构造函数用于 Block

    Block::Block(BlockValue blockValue) { setValue(blockValue); }

您会推荐哪种解决方案

编辑:我忘记提及该方法有权访问 Block 数组非常重要。这就是为什么我在第一个方法的参数列表中包含 Map &map 的原因。我为这个可怕的错误道歉。

There is a two dimensional array of Blocks in the class Map. I need a method to change the variable value in the class Block.

class Block
{
    Block(BlockValue blockValue = BLANK);

    //first solution
    void Block::setValue(BlockValue blockValue, Map &map);

    void setImage(BlockValue blockValue, int n = 0);

    //the value to be changed
    BlockValue value;

    static std::vector< std::vector<sf::Image> > images;

    ...
};

 class Map
 {
     std::vector< std::vector<Block> > blocks;

     //second solution
     void setBlockValue(BlockValue blockValue, int i, int j);

     ...
 };

I can think of two solutions:

  1. I create a Block::setValue(BlockValue blockValue, Map &map) in the class Block and call it from the Map in the following way:

    blocks[i][j].setValue(blockValue, this);

  2. I create a setBlockValue(BlockValue blockValue, int i, int j) method int the class Map. The problem with this solution is that I can't use my constructor for Block:

    Block::Block(BlockValue blockValue) { setValue(blockValue); }

Which solution would you recommend?

EDIT: I forgot to mention that it is important that the method have access to the array of Blocks. That's why I included the Map &map in the parameter list of the first method. I apologize for this terrible mistake.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

む无字情书 2024-12-19 09:00:26

你的问题不太清楚,从清晰的角度来看,我会把两者结合起来。例如

class Block
{
  public:
    // setter to set the block value
    void setValue(BlockValue blockValue);

  private:
    //the value to be changed
    BlockValue value;
};

,在 Map 中有一个方法可以为特定位置设置 BlockValue

class Map
{
  public
    void setBlockValue(BlockValue blockValue, int i, int j)
    {
      blocks[i][j].setValue(blockValue); // call the specific setter
    }

  private:
    std::vector< std::vector<Block> > blocks;
};

Your question is not very clear, from a clarity point of view, I'd combine both. e.g.

class Block
{
  public:
    // setter to set the block value
    void setValue(BlockValue blockValue);

  private:
    //the value to be changed
    BlockValue value;
};

and in Map have a method to set the BlockValue for a specific location

class Map
{
  public
    void setBlockValue(BlockValue blockValue, int i, int j)
    {
      blocks[i][j].setValue(blockValue); // call the specific setter
    }

  private:
    std::vector< std::vector<Block> > blocks;
};
神爱温柔 2024-12-19 09:00:26

您可以通过以下方式减少代码行数并提高效率:

class Map
 {
     std::vector< std::vector<Block> > blocks;
     ...

     Block& block(int i, int j) {
         return blocks[i*num_cols + j];
     }
 };

返回引用将允许调用者使用赋值,

mymap.block(i, j) = ...;

并且使您不必为读取和写入提供单独的调用。

You can reduce the number of lines of code and also improve efficiency with:

class Map
 {
     std::vector< std::vector<Block> > blocks;
     ...

     Block& block(int i, int j) {
         return blocks[i*num_cols + j];
     }
 };

Returning a reference will allow the caller to use assignment

mymap.block(i, j) = ...;

and will save you from having to provide separate calls for reading and writing.

红玫瑰 2024-12-19 09:00:26

我认为第一种解决方案更灵活。
如果 Block 会将其自身传递到映射,您可以在插入映射前或插入后时刻执行您想要的操作。

I think first solution more flexible.
If Block will past it self to map, your can do what you want on pre insert to map, or on post insert moments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文