BeeBand
BeeBand

Reputation: 11483

How do you initialise a vector of structures that contain vectors as members?

I'm using Boost::assign to initialise a vector of structures as follows:

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. The spriteInfo struct currently looks like this:

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

However, I'd like to make a std::vector<int> as a member of spriteInfo.

If I do that, how would the assignment above look? i.e. Can you initialise a vector whilst passing it as a parameter to a constructor?

Upvotes: 3

Views: 752

Answers (1)

icecrime
icecrime

Reputation: 76745

This should do :

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

Upvotes: 3

Related Questions