gpt4 book ai didi

c++ - C++ 中指针指向指针(AnyType **var)的问题

转载 作者:行者123 更新时间:2023-11-28 01:09:29 25 4
gpt4 key购买 nike

如您所知,.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com