0

如何创建对象数组并添加对象,如下所示:

Student* student11 = new Student("Vince", "Vaughn", "7-th Avenue", "New York", "783-945-90-28", 49);
    Student* student12 = new Student("Vince", "Mcmahon", "Beverly Hills", "Los Angeles", "874-940-42-12", 47);
    Student* student13 = new Student("Stone Cold", "Steve Austin", "Dallas", "Texas", "385-421-47-95", 34);
4

1 回答 1

2

看来你的意思如下

Student *student = new Student[3] 
{
    { "Vince", "Vaughn", "7-th Avenue", "New York", "783-945-90-28", 49 },
    { "Vince", "Mcmahon", "Beverly Hills", "Los Angeles", "874-940-42-12", 47 },
    { "Stone Cold", "Steve Austin", "Dallas", "Texas", "385-421-47-95", 34 }
};

Student如果类没有默认构造函数并且您需要重新分配数组,则可能会出现问题。

因此请注意,您可以使用标准模板类,而不是“手动”分配数组std::vector<Student>

于 2020-01-15T16:59:18.893 回答