new(float *)[]和new float * []之间的区别

时间:2017-10-11 18:55:00

标签: c++ arrays pointers

new int*[]new (int*)[]

之间有什么区别?
int** pA = new int*[2] // returns a pointer to an array of pointers to int.

new (int*)[2] // what should this return ? Shouldn't it be same as the above. 

同样地,

float* pB1 = new float[3]; // compiles successfully.

float* pB2 = new (float)[3] //Gives an error. Shouln't it return an array of floats ?

但是,编译器说:

A value of type 'float' cannot be used to initialize a value of type float* .

我在这里缺少什么?我正在使用VS2015社区IDE。

1 个答案:

答案 0 :(得分:6)

float* pB1 = new float[3];

正在分配一个float

数组
float* pB2 = new (float)[3]

要求分配一个数组?在'浮动'位置(这是没有意义的),这是你得到的错误。 这是展示位置新语法,请参阅评论中指出的Difference between new (float*)[] and new float*[]http://en.cppreference.com/w/cpp/language/new更多信息