在C++中,结构体可以通过以下几种方式进行初始化:
struct Person {
std::string name;
int age;
};
Person p = {"John", 30};
Person p = {};
struct {
std::string name;
int age;
} p = {"John", 30};
struct Person {
std::string name;
int age;
Person(const std::string& n, int a) : name(n), age(a) {}
};
Person p("John", 30);