在 C++ 中,对象创建括号前的星号是什么意思?
What does asterisk before brackets on object creation mean in C++?
我正在从一个网站上阅读一个用 C++ 实现的散列 table 示例,然后看到了这个。
private:
HashEntry **table;
public:
HashMap() {
table = new HashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
语法我不明白的行是:
table = new HashEntry*[TABLE_SIZE];
像这样在括号前加上星号是什么意思?
new HashEntry*[TABLE_SIZE]
分配并构造一个 TABLE_SIZE
元素的数组,其中每个元素都是一个 HashEntry*
,即指向 HashEntry
.
的指针
一个更现代的 C++ 版本是:
private:
std::vector<std::unique_ptr<HashEntry>> table;
public:
HashMap() : table(TABLE_SIZE) {}
这避免了必须定义您自己的析构函数,并且通常更安全。
星号表示是指针
这里有一些链接
http://www.cprogramming.com/tutorial/c/lesson6.html
我正在从一个网站上阅读一个用 C++ 实现的散列 table 示例,然后看到了这个。
private:
HashEntry **table;
public:
HashMap() {
table = new HashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
语法我不明白的行是:
table = new HashEntry*[TABLE_SIZE];
像这样在括号前加上星号是什么意思?
new HashEntry*[TABLE_SIZE]
分配并构造一个 TABLE_SIZE
元素的数组,其中每个元素都是一个 HashEntry*
,即指向 HashEntry
.
一个更现代的 C++ 版本是:
private:
std::vector<std::unique_ptr<HashEntry>> table;
public:
HashMap() : table(TABLE_SIZE) {}
这避免了必须定义您自己的析构函数,并且通常更安全。
星号表示是指针
这里有一些链接
http://www.cprogramming.com/tutorial/c/lesson6.html