1
votes

Saw some codes like below in a C++ project:

struct Foo{ 
    std::wstring x; 
    //blah
}

// this func returns a Foo object 

Foo getFoo(){ 
    //blah 
}

void main() { 
    Foo obj{getFoo()}; //why can initialize by another Foo object in {}? 
}

{} is list-initialization. But no Foo arguments are listed here. Why does this work? Does struct have default copy constructor?

And does Foo obj(getFoo()) work? Any difference from the way of using {}?

1
1. Foo has an implicitly declared copy constructor; 2. Foo obj(getFoo()) works too; 3. Same effect here. - songyuanyao

1 Answers

1
votes