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

发布于 2024-11-10 03:39:33 字数 524 浏览 2 评论 0原文

如果我有:

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]}

但我不能做:

a_class() {my_structs = new a_struct(1) [10]}
//or
a_class() {my_structs = new a_struct() [10]}

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

If I have:

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() {...}
};  

I can do:

a_class() {my_structs = new a_struct(1)}
//or  
a_class() {my_structs = new a_struct [10]}

But I cannot do:

a_class() {my_structs = new a_struct(1) [10]}
//or
a_class() {my_structs = new a_struct() [10]}

Is there any correct syntax to get this to work? Or an easy work around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぇ气 2024-11-17 03:39:33

如果可以选择使用 STL,则可以使用 std::vector 而不是动态数组。

我认为这会起作用:

std::vector<a_struct> my_structs;

my_structs.assign(10, 1);

如果没有,这应该:

my_structs.assign(10, a_struct(1));

If using the STL is an option, you could use std::vector instead of a dynamic array.

I think that this will work:

std::vector<a_struct> my_structs;

my_structs.assign(10, 1);

If not, this should:

my_structs.assign(10, a_struct(1));
挽梦忆笙歌 2024-11-17 03:39:33

您可以分配原始内存块并使用 placement new 来初始化每个struct

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++;
}

//When done should add code for deallocation to help people 
// to understand the full cycle of memory management.  
for (auto i=0; i<number_of_structs; ++i) {
    my_structs[i].~a_struct(); 
} 
delete[] my_structs;

另请参阅:“展示位置新”有什么用途?

You could allocate a raw chunk of memory and use placement new to initialize each struct:

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++;
}

//When done should add code for deallocation to help people 
// to understand the full cycle of memory management.  
for (auto i=0; i<number_of_structs; ++i) {
    my_structs[i].~a_struct(); 
} 
delete[] my_structs;

See also: What uses are there for "placement new"?

大海や 2024-11-17 03:39:33

您可以使用指向指针的指针数组。然后你可以创建一个数组来保存指向 a_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);
    }
}; 

You could use an array of pointers to pointers. Then you can create the array that will hold pointers to a_struct(), so you can decide later which constructor to use:

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);
    }
}; 
野侃 2024-11-17 03:39:33

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

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

我建议使用 std::vector 容器而不是经历这个过程。

You can't do it directly on any particular parameterized constructor. However you can do,

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

I suggest using a std::vector container instead of going through this process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文