vendredi 17 mars 2023

cpp11 assign values for struct with different name but exactly same element?

i got two structs, some part of the two struct have exactly same element type (and name), how can i assign one with another's value?

as below demostrated:


struct X
{
    struct
    {
        int a = 0;
        int b = 0;
    } z;
    int c = 0;
};


struct Y
{
    int a = 0;
    int b = 0;
};


int main()
{
    
    X x{1,2,3};
    Y y;
    
    // i know i can do 
    y.a = x.z.a;
    y.b = x.z.b;
    
    // but is there a simple way like:
    // y = x.z;
    
}

i looked into memcpy but in my real case, the X is much more complicated and dynamic length, memcpy looks not quit help for me. thx for any advice

Aucun commentaire:

Enregistrer un commentaire