lundi 22 juin 2015

confusing struct initializations in C++

Consider following program:

#include <iostream>
struct Test {
    int x;
    int y;
};
int main()
{
    Test a={6,9};        // aggregate initialization
    Test b;
    b=Test{15,30};       // What is this? What it does?
    Test c;
    c={11,4};            // What is this?
    std::cout<<a.x<<' '<<a.y<<'\n';
    std::cout<<b.x<<' '<<b.y<<'\n';
    std::cout<<c.x<<' '<<c.y<<'\n';
}

What is the exact meaning of 3rd statement in main()? Why it is allowed? Is there any temporary object created by compiler? What is the meaning of c={11,4}; ? What is the difference between Line 3 & Line 5 in main() function? I mean, how following 2 statements are different?

b=Test{15,30};

c={11,4};

Is it allowed in C language also? Is this new C++0x introduced feature? I am really getting confused.

Aucun commentaire:

Enregistrer un commentaire