Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++: Initialize struct with incomplete type

I have two types defined in a header file like so:

struct vec2{
    float x;
    float y;
    vec2() : x(0), y(0) {}
};
struct vec3{
    float x;
    float y;
    float z;
    vec3() : x(0), y(0), z(0) {}
};

In some C++ file I would like to be able to write:

vec2 a2 = {2,4};
vec3 a3 = vec2(a2);
//This would give me a3 with a z-value of 0.

Which would work by adding this to the header:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 vec3(vec2 a) : x(a.x), y(a.y), z(0) {}

But I would also like to be able to write:

vec3 a3 = {1,2,4};
vec2 a2 = vec3(a3); //Where this would result in a3's z component being dropped.

Is this possible? Would I need to typedef it somehow so the compiler knows ahead of time what size the 2 structures are?

>Solution :

No problem, you have to set the constructors up to perform implicit conversions:

namespace vec{ // if in a header, this should avoid eventual name conflicts
    struct vec3;   // forward declaration

    struct vec2{
        float x;
        float y;
        vec2(float inp1 = 0, float inp2 = 0): 
            x(inp1), y(inp2) {}
        vec2(const vec3& inp);   // vec3 forward declared
    };

    struct vec3{
        float x;
        float y;
        float z;
        vec3(float inp1 = 0, float inp2 = 0, float inp3 = 0): 
            x(inp1), y(inp2), z(inp3) {}
        vec3(const vec2& inp): 
            x(inp.x), y(inp.y), z(0) {}
    };

    vec2::vec2(const vec3& inp): 
        x(inp.x), y(inp.y) {}
}

Test function:

int main()
{
    using namespace vec;
    vec3 a = {1,2,3};
    vec2 a_minus(a);
    vec3 a_again(a_minus);
    
    std::cout << a_minus.x << ", " << a_minus.y << ", " << a_again.z <<'\n';

    return 0;
}

Output:

1, 2, 0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading