可以为静态数组的定义提供初始化列表.例:
int main()
{
int int_static[2] = {1,2};
}
Run Code Online (Sandbox Code Playgroud)
动态数组是否可以使用类似的初始化列表?
int main()
{
int* int_ptr = new int[2];
}
Run Code Online (Sandbox Code Playgroud)
这更接近我想要做的事情:
struct foo
{
foo(){}
foo(void * ptr): ptr_(ptr) {}
void * ptr_;
};
int main()
{
foo* foo_ptr = new foo[10];
}
Run Code Online (Sandbox Code Playgroud)
在初始化时,不应该调用默认构造函数,而是调用foo:foo(void*).
对于动态数组的静态初始化程序列表而言,如果加速器核心的实时编译只有有限的堆栈可用,但同时用(加速器编译时间=主机运行时间)静态初始化列表.
我假设没有(因为这需要编译器生成额外的代码,即将参数的值复制到堆位置).我认为c ++ 0x支持其中一些,但我无法使用它.现在我可以使用这样的结构.也许有人知道一招
最好!
如果我有:
struct a_struct
{
int an_int;
a_struct(int f) : an_int(f) {}
a_struct() : an_int(0) {}
};
class a_class
{
a_struct * my_structs;
a_class() {...}
};
Run Code Online (Sandbox Code Playgroud)
我可以:
a_class() {my_structs = new a_struct(1)}
//or
a_class() {my_structs = new a_struct [10]}
Run Code Online (Sandbox Code Playgroud)
但我不能这样做:
a_class() {my_structs = new a_struct(1) [10]}
//or
a_class() {my_structs = new a_struct() [10]}
Run Code Online (Sandbox Code Playgroud)
有没有正确的语法来使这个工作?或者轻松解决?