2012-08-10 230 views
3

的陣列我有以下結構:C++結構初始化

struct localframepos 
{ 
    double ipos; //local room frame i(x) coordinate, in mm 
    double cpos; //local room frame c(y) coordinate, in mm 
    double rpos; //local room frame r(z) coordinate, in mm 

    localframepos() 
    { 
     ipos = 0; 
     cpos = 0; 
     rpos = 0; 
    } 
    localframepos(double init_ipos, double init_cpos, double init_rpos) //init constructor 
    { 
     ipos = init_ipos; 
     cpos = init_cpos; 
     rpos = init_rpos; 
    } 
}; 

我如何得到以下功能:

localframepos positions[] = { {0, .5, .2}, 
           {4.5, 4, .5} }; 

回答

3

取出構造。要使用花括號初始化,類型必須是POD。

+0

是不是 「必須是POD」? – 2012-08-10 01:16:40

+0

什麼是非POD? – CodeKingPlusPlus 2012-08-10 01:16:48

+0

@pwny true ..... – 2012-08-10 01:17:36

0

我通常使用

localframepos positions[] = { localframepos(0 , .5, .2), 
           localframepos(4.5, 4, .5) }; 

,如果你需要不同的初始化可能性,這是不是很好看,但更靈活。

0

對於C++ 03,你可以寫

localframepos positions[] = { localframepos(0, .5, .2), 
           localframepos(4.5, 4, .5) };