2013-03-16 46 views
0

我想存储结构的动态数组作为另一个结构中的成员变量。这是正确的方式来使用主给我的例子构造函数?如何初始化动态成员变量?

编辑 我纠正了我在代码中出现的一些明显的错误(当时是早上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); 
} 
+0

如果你有一个B的向量,为什么有一个A的数组? – 2013-03-16 10:41:21

回答

1

孤立地说,构造函数很好。但是,代码还存在许多其他问题。

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

您可能想要考虑从使用C阵列到std::vectorstd::array

还有在printf()的错误(如拼写错误print()):两个mem1之一应该是mem2

1

B构造函数是OK,但你调用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. 
      ); 
    } 
} 
+0

push_back和:错误都是由于在上午6点提交问题。我添加了另一个成员的结构,所以现在我必须做push_back(B(test,test1))正确吗? – postelrich 2013-03-16 15:27:21

+0

@riotburn:是的,正确的。顺便说一句,如果你有选择,更喜欢在动态分配的数组上使用'std :: vector'。手动内存管理非常容易出错,并且通常会使代码更易于维护。 – 2013-03-16 15:32:16

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风格的数组的为好。