C++ 中的对象指针的 2D 数组
2D Array of Object pointers in C++
如何将指针的 2D 数组分配给对象?
目前我有:
文件.h
extern ClassName **c;
文件.cpp
ClassName **c;
int main(){
// some stuff
c = new ClassName*[sizex];
for(int i = 0; i < sizex; ++i){
c[i] = new ClassName[sizey];
}
for(int i = 0; i < sizex; ++i){
for(int j = 0; j < sizey; ++j){
c[i][j] = new ClassName();
}
}
它无法编译错误,指出使用 ClassName 和 ClassName* 的运算符 = 不匹配,查看错误是有意义的。但是,如果我要将 c[i][j] 的赋值更改为
ClassName cn();
c[i][j] = cn;
它给出了大量其他错误。数组的大小在运行时(从标准输入)之前无法知道,并且它也必须是 extern。在这种情况下,声明数组的正确方法是什么?
你必须像这样声明指针
extern ClassName ***c;
分配将如下所示
c = new ClassName**[sizex];
for(int i = 0; i < sizex; ++i){
c[i] = new ClassName*[sizey];
for(int j = 0; j < sizey; ++j){
c[i][j] = new ClassName();
}
}
如果您声明某种抽象类型的数组T
,则可以自己正确定义 2D 数组。然后,您所需要的只是将T
更改为ClassName *
至于这个宣言
ClassName cn();
然后它声明一个返回类型为 ClassName 且没有参数的函数。
ClassName *p1;
p1
可以指向一个ClassName
或一组ClassName
。
ClassName **p2;
p2
可以指向一个ClassName*
或一个ClassName*
数组。
*p2
可以指向一个ClassName
或一个ClassName
数组。
当您使用:
c[i] = new ClassName[sizey];
您正在分配内存,以便c[i][j]
可以容纳ClassName
但不能容纳ClassName*
。
如果c[i][j] = ClassName();
失败,并且您想使用 c[i][j] = new ClassName();
,则必须将c
声明为:
ClassName*** c;
但是,为了不这样做,我强烈建议使用std::vector
和智能指针。
std::vector<std::vector<std::unique_ptr<ClassName>>> c;
前面的海报已经正确回答了使用 tripple 指针,但是为了你的理智和代码的清晰度,你可以使用几个简单的typedefs:
typedef ClassName* Row;
typedef Row* Matrix;
Matrix *M; //equivalent to : ClassName ***M,
//check by substiting Matrix with Row* and Row with ClassName*
int main()
{
M = new Matrix[numRows];
for(int row = 0; row < numRows; ++row)
{
M[row] = new Row[numCols];
for(int col = 0; col < numCols; ++j)
{
M[row][col] = new ClassName();
}
}
}
这样可以更好地传达您的意图,并且更容易推理。
您需要将类型更改为:类名 ***c; 正如来自莫斯科的弗拉德所提到的。
ClassName **c;
int main(){
// some stuff
c = new ClassName**[sizex];
for(int i = 0; i < sizex; ++i){
c[i] = new ClassName*[sizey]
}
for(int i = 0; i < sizex; ++i){
for(int j = 0; j < sizey; ++j){
c[i][j] = new ClassName();
}
}
您的用法也必须改变:
ClassName cn;
c[i][j] = new ClassName( cn ); // <-- this copy constructor would cause a memory leak (old pointer not deleted)
*(c[i][j]) = cn; // <-- this would use the assignment operator. May be weak performance.
ClassName & cn = *(c[i][]); // <-- Allows you to work directly on the cell.
>ClassName **c
是一个 2D 数组,即c[n][m]
ClassName
对象。
您试图将类型 ClassName
的c[n][m]
指定为类型 ClassName*
的new ClassName()
for(int i = 0; i < sizex; ++i){
for(int j = 0; j < sizey; ++j){
c[i][j] = *(new ClassName);
}
}
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 使用指针算法修改函数中的 2D 数组
- 在C++中初始化和打印 2D 数组
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- C++ 2D 数组写入多个位置
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 如何从initializer_list初始化 2D 数组
- 交换函数不是在 C++ 中交换 2D 数组的元素