如何初始化动态成员变量?

时间:2013-03-16 10:06:10

标签: c++ dynamic struct

我想将一个动态结构数组存储为另一个结构中的成员变量。这是在main中给出我的示例使用构造函数的正确方法吗?

修改 我纠正了代码中的一些明显错误(当时是早上6点)。我还向B添加了另一个成员,看看push_back是否仍然正确。我知道使用矢量动态记忆会更容易,但我需要这样做,因为这些结构最终会与thrust :: device_vector一起使用。

struct A
{
    float mem1;
    int mem2;
};
struct B
{
    A * Aarr1;
    A * Aarr2;
    B(A * a1, A * a2): Aarr1(a1), Aarr2(a2){}
};
int main()
{
    A * test = new A[5];
    A * test2 = new A[10];
    vector<B> btest;
    btest.push_back(B(test, test2));
    for(int i=0; i<5; i++)
        printf("mem1: %f, mem2: %i \n", btest[0].Aarr[i].mem1, btest[0].Aarr[i].mem2);
}

3 个答案:

答案 0 :(得分:1)

孤立地采取,构造函数很好。但是,代码还有许多其他问题。

目前,您的代码正在泄漏内存,因为数组永远不会被释放。

您可能需要考虑从使用C数组转移到std::vectorstd::array

printf()中还有一个错误(print()拼写错误):其中一个mem1应为mem2

答案 1 :(得分:1)

B的构造函数没问题,但调用push_back()的方式不是:

btest.push_back(B(A));

你应该这样做:

btest.push_back(B(test));

此外,没有必要显式构造B对象,因为您的构造函数未标记为explicit

btest.push_back(test);

还要考虑使用自动内存管理而不是原始指针(std::vector<>而不是数组,智能指针而不是指针)。这样,你就可以避免因忘记这一点而泄漏内存:

delete test;

漏掉一边,最糟糕的是你的代码也有未定义的行为,因为它使用了未初始化的变量值(A循环中for的成员变量)。

最后,您不应在类定义中使用类名后的:。这就是你可以用C ++ 11重写代码的方法:

#include <vector>
#include <cstdio>

struct A // Do not use ":" here, that's for introducing inheritance,
         // which you are not using in your example.
{
    float mem1 = 0.0; // In C++11, you can specify default initialization
    int mem2 = 0;     // for your member variables this way. In C++03 you
                      // would have to define a default constructor which
                      // initializes your member variables to the desired
                      // value.
};

struct B
{
    std::vector<A> Aarr;
//  ^^^^^^^^^^^^^^
//  Prefer using standard containers over dynamically allocated arrays, as
//  it saves your from taking care of memory management and avoids leaks.

    explicit B(size_t s): Aarr(s) { }
//  ^^^^^^^^
//  It is usually a good idea to mark constructors which take on argument
//  and are not copy constructors as explicit, to avoid awkward implicit
//  conversions.
};
int main()
{
    std::vector<B> btest;
    btest.push_back(B(5));
//                  ^^^^
//                  We need to explicitly construct an object of type B,
//                  because we have marked B's constructor as explicit.

    for(int i=0; i<5; i++)
    {
        std::printf(
             "mem1: %f, mem2: %i \n", 
            btest[0].Aarr[i].mem1, 
            btest[0].Aarr[i].mem2
            //                  ^
            //                  You had "mem1" here.
            );
    }
}

答案 2 :(得分:0)

您的代码中存在一些小错误和拼写错误。它应该是这样的:

struct A  // <-- no ':' after name type
{
    float mem1;
    int mem2;
};

struct B  // <-- no ':' after name type
{
    A * Aarr;
    B(A * a): Aarr(a){}
};

int main()
{
    A * test = new A[5];
    vector<B> btest;
    btest.push_back(B(test)); // <-- test, A is name of type
    for(int i=0; i<5; i++)
        printf("mem1: %f, mem2: %i \n", // <-- printf, not print
                btest[0].Aarr[i].mem1, btest[0].Aarr[i].mem1);
}

同时考虑使用std::vectorstd::array代替C风格的数组。

相关问题