CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

C++ array of structures initialization

I have the following structure:

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

How do I get the following functionality:

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

Upvotes: 3

Views: 1550

Answers (3)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

For C++03 you can write

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

Upvotes: 0

klm123
klm123

Reputation: 12915

I usually use

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

which is not that nice looking, but much more flexible if you need different initialization possibilities.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258678

Remove the constructors. To use curly brace initialization, the type has to be a POD.

Upvotes: 3

Related Questions