如您所知,.NET 框架中有一个通用类 List。 我想用 C++ 编写一个通用类列表,我想在我的列表中存储指向一个类型的指针。这是类和测试程序的头文件和源代码:
// header
template <class Type>
class List
{
public:
List(int size); // constructor
.
.
. // other public members
private:
Type **list; // a dynamic array of pointer to Type
.
.
. // other private members
};
// source code
template <class Type> List<Type>::List(int size) // constructor
{
this->list = new Type[size];
.
. // other parts of definition
}
// main function
void main()
{
List<AnyType> mylist = new List<AnyType>(4);
mylist[0] = new AnyType( // any arguments);
}
它不能正常工作。问题在哪里?是否可以将此类用于结构?
最佳答案
this->list = new Type[size];
应该是 this->list = new Type*[size];
编辑:它真的编译了吗?作业至少应生成警告。
关于c++ - C++ 中指针指向指针(AnyType **var)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4142453/