有两个版本:
wsk = new unsigned int;
wsk = new unsigned int();
也适用于数组:
wsa = new unsigned int[5];
wsa = new unsigned int[5]();
在回答下面的评论。
嗯...您确定new unsigned int[5]()
将整数归零吗?
显然是的:
[C ++ 11:5.3.4 / 15]:创建类型T的对象的new表达式按如下方式初始化该对象:如果省略了new-initializer,则该对象将被默认初始化(8.5);否则,该对象将被初始化。如果未执行初始化,则该对象具有不确定的值。否则,将根据8.5的初始化规则对新初始化程序进行解释以进行直接初始化。
#include <new>
#include <iostream>
int main()
{
unsigned int wsa[5] = {1,2,3,4,5};
unsigned int* wsp = new (wsa) unsigned int[5]();
std::cout << wsa[0] << "\n";
std::cout << wsa[1] << "\n";
std::cout << wsa[2] << "\n";
std::cout << wsa[3] << "\n";
std::cout << wsa[4] << "\n";
}
结果:
> g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
> g++ t.cpp
> ./a.out
0
0
0
0
0
>