Initializing structs in C++

后端 未结 5 904
余生分开走
余生分开走 2020-12-30 21:19

As an addendum to this question, what is going on here:

#include 
using namespace std;

struct A {
    string s;
};

int main() {
    A a = {0}         


        
5条回答
  •  离开以前
    2020-12-30 21:59

    As people have pointed out, this "works" because string has a constructor that can take 0 as a parameter. If we say:

    #include 
    using namespace std;
    
    struct A {
        map  m;
    };
    
    int main() {
        A a = {0};
    }
    

    then we get a compilation error, as the map class does not have such a constructor.

提交回复
热议问题