通过首先将 2D 数组发送到 C++ 中的超类来初始化子类?

发布于 2025-01-07 01:02:48 字数 1602 浏览 0 评论 0 原文

我正在尝试用 C++ 编写以下 Java 代码:

public class Block {
    private int[][] block;
    public Block(int[][] block) {
        this.block = block;
    }
}

public class Block1 extends Block {
    public Block1() {
        super(new int[][]{{2,3},{3,1}});
    }
}

下面您可以看到我将代码翻译为 C++ 的进展。基本上我希望 Block 成为一个通用类,帮助继承类(目前只有 Block1)将有关它们的数据存储为 2D 数组。这些数组应该在子类的构造函数中初始化。 但我无法让 super 调用在 Block1 中正常工作,我也不知道应该如何发送 2D 数组作为参数,或者如何再次正确存储对其的引用。如果你们能向我展示这个,那就太好了。

(稍后我将在类中添加更多方法 - 这只是与问题相关的最重要的内容。)

Block.h:

#ifndef BLOCK_H_
#define BLOCK_H_

namespace mtch {

class Block {
public:
    Block(int _block[][2]);
    virtual ~Block();
private:
    int block[2][2];
};

} /* namespace mtch */
#endif /* BLOCK_H_ */

Block.cpp:

#include "Block.h"

namespace mtch {

Block::Block(int _block[][2]) {
    block = _block;
}

} /* namespace mtch */

Block1 .h:

#include "Block.h"

#ifndef BLOCK1_H_
#define BLOCK1_H_

namespace mtch {

class Block1 : public Block {
public:
    Block1();
    virtual ~Block1();
};

} /* namespace mtch */
#endif /* BLOCK1_H_ */

Block1.cpp:

#include "Block1.h"

namespace mtch {

Block1::Block1() : Block(new int[][]{{2,2},{2,3}}) {
}

}

我希望您理解我在这里想要完成的任务。 :P

I'm trying to code the following Java code in C++:

public class Block {
    private int[][] block;
    public Block(int[][] block) {
        this.block = block;
    }
}

public class Block1 extends Block {
    public Block1() {
        super(new int[][]{{2,3},{3,1}});
    }
}

Below you can see how far I have come with my translation of the code to C++. Basically I want Block to be a general class which helps the inheriting classes (currently only Block1) to store data about them as 2D arrays. These arrays should be initialized right in the subclasses' contructor's.
But I can't get the super call to work correctly in Block1, neither do I know how I should send a 2D array as a parameter, or how to correctly store the reference to it again. It would be awesome if you guys could show me this.

(I will add more methods later to the classes - this is only the most essential stuff related to the problem.)

Block.h:

#ifndef BLOCK_H_
#define BLOCK_H_

namespace mtch {

class Block {
public:
    Block(int _block[][2]);
    virtual ~Block();
private:
    int block[2][2];
};

} /* namespace mtch */
#endif /* BLOCK_H_ */

Block.cpp:

#include "Block.h"

namespace mtch {

Block::Block(int _block[][2]) {
    block = _block;
}

} /* namespace mtch */

Block1.h:

#include "Block.h"

#ifndef BLOCK1_H_
#define BLOCK1_H_

namespace mtch {

class Block1 : public Block {
public:
    Block1();
    virtual ~Block1();
};

} /* namespace mtch */
#endif /* BLOCK1_H_ */

Block1.cpp:

#include "Block1.h"

namespace mtch {

Block1::Block1() : Block(new int[][]{{2,2},{2,3}}) {
}

}

I hope you understand what I'm trying to accomplish here. :P

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

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

发布评论

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

评论(1

吻风 2025-01-14 01:02:48

block 是您定义的静态数组,您不能简单地为其分配动态数组以将指针弯曲到正确的内存位置。您必须将每个元素复制到块中。最简单的方法是使用 std::vector> 而不是 int[2][2] 作为块。另一种方法是到处使用简单的一维数组。您可以创建 int* 类型的块,并使所有函数也采用 int* 。然后你可以在Block1()中new int[4]并初始化Block1()体内的内存。此外,您还必须确保删除[]内存,因为它不会像静态数组那样自动发生。多维数组有点令人讨厌

block is a static array as you defined it, you can't simply assign a dynamic array to it to bend pointers to the right memory location. You would have to copy each element into block. Easiest way would be to use std::vector<std::vector<int>> instead of int[2][2] for block. Another way would be to use a simple one dimensional array everywhere. you'd make block of type int* and make all the functions take int* too. Then you can just new int[4] in Block1() and initialize the memory inside Block1()'s body. Also you would have to make sure to delete[] the memory because it won't happen automagically like it does for the static array. Multidimensional arrays are a bit icky

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