new后面的构造函数如果是数组是不是不能赋初值
1个回答
展开全部
可以赋初值的。
例如:
#include <iostream>
using namespace std;
class Person
{
private:
char* name;
int age;
public:
Person(char* name, int age)
{
this->name = name;
this->age = age;
}
void show()
{
cout << "name: " << name << endl;
cout << "age: " << age << endl << endl;
}
};
int main()
{
Person p[3] = {Person("zhangsan", 20), Person("lisi", 21), Person("wanger", 22)};
p[0].show();
p[1].show();
p[2].show();
getchar();
return 0;
}
例如:
#include <iostream>
using namespace std;
class Person
{
private:
char* name;
int age;
public:
Person(char* name, int age)
{
this->name = name;
this->age = age;
}
void show()
{
cout << "name: " << name << endl;
cout << "age: " << age << endl << endl;
}
};
int main()
{
Person p[3] = {Person("zhangsan", 20), Person("lisi", 21), Person("wanger", 22)};
p[0].show();
p[1].show();
p[2].show();
getchar();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询