c++ - 使用 Type** &x 创建动态数组

标签 c++ templates pointers

关于以下制作动态数组的代码片段

template <class Type> 
void Make2DArray(Type** &x,int rows, int cols) 
{ x=new Type*[rows]; 
   for (int i=0;i<rows;i++) x[i]=new Type[cols]; 
}

如何理解Type** &x的用法,为什么会有两个**

最佳答案

Type** 是指向指针的指针。

Type**& 是对指向指针的指针的引用。

** 将允许您创建一个数组的数组。这可用于二维数组 - x[][] 将起作用。

关于c++ - 使用 Type** &x 创建动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720419/

相关文章:

c++ - 转发参数的可变参数列表

C 函数指针

c++ - 如何在 C++ 中比较两个 NAN 值

c++ - 如何让 C++ 更喜欢将 char* 转换为 string_view 而不是 bool?

c++ - 模板函数中的 "C4430: missing type specifier - int assumed"

C++ - 在构造函数中初始化指针数组

c++ - 我是否需要为指向构造函数的 char 指针分配内存?

c++ - 多层继承中的virtual关键字

c++ - C++ 中的 typedef 和运算符重载

C++0x 可变参数模板通过引用传递