在定长数组typedef中使用new [英] Using new with fixed length array typedef
问题描述
如何为固定长度的数组定义typedef,以便也可以新建".以下内容不起作用:
How do I define a typedef for a fixed length array so that I can also 'new'. The following does not work:
typedef double Vector[3];
Vector *v = new Vector; // does not compile
我们正在尝试将一些旧的C代码包装到C ++中,以通用方式处理 float *
和 float(*)[3]
.
We are trying to wrap into C++ some old C code which handles float *
and float (*)[3]
in a generic way.
推荐答案
指向 double [3]
的指针是 double *
-这样就可以了:>
The pointer to an double[3]
is double *
- so this will work:
typedef double Vector[3];
double *v = new Vector;
但是我建议您不要那样使用-删除数组需要使用array-delete-operator:
But I suggest you don't use it that way - to delete the array you need the array-delete-operator:
delete[] v;
但是在 new Vector
上,您看不到它是一个数组,因此可能会忘记它.
But on new Vector
you don't see it is an array and so it might be forgotten.
Scott Meyers 有效C ++ 处理(强烈建议避免这种情况).所以最好不要在这里使用typedef.
This case is handled (and strongly recommended to avoid) in Scott Meyers Effective C++. So better don't use an typedef here.
这篇关于在定长数组typedef中使用new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!