Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:使用非默认构造函数动态分配结构的成员数组_C++_Arrays_Constructor_Struct_Memory Management - Fatal编程技术网

C++ C++;:使用非默认构造函数动态分配结构的成员数组

C++ C++;:使用非默认构造函数动态分配结构的成员数组,c++,arrays,constructor,struct,memory-management,C++,Arrays,Constructor,Struct,Memory Management,如果我有: 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() {...} }; 我可以做到: a_class() {my_structs = new a_struct(1)} //or a_class() {my_s

如果我有:

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() {...}
};  
我可以做到:

a_class() {my_structs = new a_struct(1)}
//or  
a_class() {my_structs = new a_struct [10]}
但我不能这样做:


有没有正确的语法让它工作?或者一个简单的解决方法?

如果使用STL是一个选项,那么可以使用std::vector而不是动态数组

我认为这将起作用:

std::vector<a_struct> my_structs;

my_structs.assign(10, 1);

可以使用指向指针的指针数组。然后,您可以创建一个数组,该数组将保存指向_struct()的指针,以便以后决定使用哪个构造函数:

class a_class {
    a_struct ** my_structs;

    a_class() { my_structs = new a_struct* [10]}
    void foo () {
       my_structs[0] = new a_struct(1);
       my_structs[5] = new a_struct("some string and float constructor", 3.14);
    }
}; 

不能直接在任何特定的参数化构造函数上执行此操作。不管你怎么做

a_struct *my_struct[10] = {}; // create an array of pointers

for (int i = 0; i < 10; i++)
    my_struct[i] = new a_struct(i); // allocate using non-default constructor
a_struct*my_struct[10]={};//创建一个指针数组
对于(int i=0;i<10;i++)
my_struct[i]=新的a_struct(i);//使用非默认构造函数分配
当您要取消分配内存时

for (int i = 0; i < 10; i++)
    delete my_struct[i]  // de-allocate memory
for(int i=0;i<10;i++)
删除我的结构[i]//取消分配内存

我建议使用
std::vector
容器,而不是执行此过程。

您可以分配一个原始内存块,并使用placement new初始化每个
结构:

int number_of_structs = 10;
my_structs = (a_struct*)new unsigned char[sizeof(a_struct) * number_of_structs];
     // allocate a raw chunk of memory 
a_struct* p = m_structs;
for (int i=0; i<number_of_structs; i++)
{
    new (p) a_struct(i);
    p++;
}
结构的整数=10; my_structs=(a_struct*)新的无符号字符[sizeof(a_struct)*number_of_structs]; //分配一个原始内存块 a_结构*p=m_结构;
对于(int i=0;在本例中,
std::vector
的优点是所有
my_struct
s将位于一个连续的内存块中。这不允许您传递结构数组(或指向结构的指针,然后使用指针数学移动到数组的下一个元素)或只传递
std::vector my_structs(10,1);
这不允许传递结构数组(或指向结构的指针,然后使用指针数学移动到数组的下一个元素)。
for (int i = 0; i < 10; i++)
    delete my_struct[i]  // de-allocate memory
int number_of_structs = 10;
my_structs = (a_struct*)new unsigned char[sizeof(a_struct) * number_of_structs];
     // allocate a raw chunk of memory 
a_struct* p = m_structs;
for (int i=0; i<number_of_structs; i++)
{
    new (p) a_struct(i);
    p++;
}