[英]Meaning of intrinsic type with () in C++
当内在类型具有括号运算符时,它意味着什么。
例如int ()或float ()
它是默认构造函数还是对内在类型有不同的含义?
- - 编辑 - -
要添加更多上下文:
在我的C ++学校教科书中,我正在讨论模板。 用于演示的模板是表格模板(2D阵列)。 Table的一个函数是用于更改表的维度的resize方法,也用于Table的一些构造函数。 有问题的构造函数和resize方法是:
template <typename T>
Table<T>::Table<T>(int m, int n)
{
mDataMatrix = 0;
mNumRows = 0;
mNumCols = 0;
resize(m, n, T());
}
和
template <typename T>
void Table<T>::resize(int m, int n, const T& value)
{
// Destroy the previous data.
destroy();
// Save dimensions.
mNumRows = m;
mNumCols = n;
// Allocate a row (array) of pointers.
mDataMatrix = new T*[mNumRows];
// Now, loop through each pointer in this row array.
for(int i = 0; i < mNumRows; ++i)
{
// And allocate a column (array) to build the table.
mDataMatrix[i] = new T[mNumCols];
// Now loop through each element in this row[i]
// and copy 'value' into it.
for(int j = 0; j < mNumCols; ++j)
mDataMatrix[i][j] = value;
}
}
在构造函数定义中, resize的第三个参数是T ()(我假设T变为模板的指定类型)。
在resize的定义中, T ()用于value参数,以便为表中的元素分配默认值。
从一些早期的答案中,这是零初始化。 我假设这意味着表中每个元素的值为0(或者,如果类型是字符串 ,则表示等效的0,我猜)。 这个对吗?
实际上,您的代码与您声明int
或float
时的代码有所不同。 零初始化数字。
例如,这三行是等价的:
int a = int();
int a = int{};
int a{};
所有这三个都是零初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.