Constructor + putting into an array

Started by
3 comments, last by King Mir 8 years, 2 months ago

i have such constructor








THullPolygon(float subarea, vec3 cp, vec3 A, vec3 B, vec3 C, vec3 A2, vec3 B2, vec3 C2) : submerged_area(subarea), center_point(cp),
TrisVerts[0](A), TrisVerts[1](B), TrisVerts[2](C), TrisVerts[3](A2), TrisVerts[4](B2), TrisVerts[5](C2)
{
pressure = 0.0;
pressure2 = 0.0;
tris = 2;
}

where


vec3 TrisVerts[6];

now compiler throws ( expected at




TrisVerts[0](A), TrisVerts[1](B), TrisVerts[2](C), TrisVerts[3](A2), TrisVerts[4](B2), TrisVerts[5](C2)

but when i change that to




ka(A), kb(B), kc(C), ka2(A2), kb2(B2), kc2(C2)

where




vec3 ka, kb, kc;
vec3 ka2, kb2, kc2;

it compiles, so is there a way to initialize an array of do i have to do this by declaring 6 different vars?

Advertisement

it compiles, so is there a way to initialize an array of do i have to do this by declaring 6 different vars?

No, at least in C++11 you can do so:


class Testing
{
public:

    Testing(float x, float y, float z) :
        arr { x, y, z}
    {
    }

    float arr[3];
};

Instead of () you use {}, and pass the elements in the desired order.

ok thanks

The non C++11 way is to stick the code in the constructor instead of the initialiser list, no need for 6 extra vars in that case either.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Also consider using std::array in C++11. It's an array that behaves more like a normal type, specifically without implicit pass by reference.

This topic is closed to new replies.

Advertisement