结构体初始化会调用构造函数
using namespace std;
class cls{
public:
cls(){
c = 5;
cout << "cls init" << endl;
};
int c;
};
struct s{
s(){
cout << a << endl;
cout << b.c << endl;
};
int a;
cls b;
};
int main(){
s s1;
}
结构体构造函数中未赋值所有的变量,其中class会调用该类的构造函数
using namespace std;
class cls{
public:
cls(){
c = 5;
cout << "cls init" << endl;
};
int c;
};
struct s{
s(int c): a(c){
cout << a << endl;
cout << b.c << endl;
};
int a;
cls b;
};
int main(){
s s1(4);
}