在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);