2010-12-09 88 views
3

我使用boost ::分配初始化結構矢量如下:如何初始化包含向量作爲成員的結構向量?

const std::vector<spriteInfo> Resources::hudObjectInfo = boost::assign::list_of 
    (spriteInfo(FALSE, SCORE_START_X, SCORE_START_Y, CENTER_ALIGN)) 
    (spriteInfo(FALSE, SCORE_START_X+NUM_DIFF, SCORE_START_Y, CENTER_ALIGN)) 
    ... /* and so on */ 
; 

NB。該spriteInfo結構目前看起來是這樣的:

struct spriteInfo 
{ 
    spriteInfo::spriteInfo(bool i, float x, float y, int align): 
     invisible(i), x1(x), y1(y), alignment(align){} 
    bool  invisible; 
    float  x1; 
    float  y1; 
    int   alignment; 
}; 

不過,我想做出std::vector<int>作爲spriteInfo成員。

如果我這樣做,上面的任務將如何查看?即你可以初始化一個向量,同時將它作爲參數傳遞給構造函數?

回答

3

這應該這樣做:

struct spriteInfo 
{ 
    spriteInfo(bool i, float x, float y, int align, const std::vector<int> &v): 
     invisible(i), x1(x), y1(y), alignment(align), something(v) {} 
    bool    invisible; 
    float   x1; 
    float   y1; 
    int    alignment; 
    std::vector<int> something; 
}; 

int main() 
{ 
    const std::vector<spriteInfo> Resources::hudObjectInfo = boost::assign::list_of 
     (spriteInfo(FALSE, SCORE_START_X, SCORE_START_Y, CENTER_ALIGN, boost::assign::list_of(1)(2)(3))) 
     (spriteInfo(FALSE, SCORE_START_X+NUM_DIFF, SCORE_START_Y, CENTER_ALIGN, boost::assign::list_of(4)(5)(6))) 
    ; 
}