如何为对象数组分配内存
How to allocate memory fo array of objects
我正在尝试用 C++ 创建一个对象数组。由于 C++ 支持 int、float 等原生对象,因此创建它们的数组不是问题。
但是当我创建一个 class 并创建该 class 的对象数组时,它不起作用。
这是我的代码:
#include <iostream>
#include <string.h>
using namespace std;
class Employee
{
string name;
int age;
int salary;
public:
Employee(int agex, string namex, int salaryx)
{
name = namex;
age = agex;
salary = salaryx;
}
int getSalary()
{
return salary;
}
int getAge()
{
return age;
}
string getName()
{
return name;
}
};
int main(void)
{
Employee **mycompany = {};
//Create a new Object
mycompany[0] = new Employee(10, "Mayukh", 1000);
string name = mycompany[0]->getName();
cout << name << "\n";
return 0;
}
没有编译错误,但是当我运行程序时,它崩溃了。我不知道这里到底发生了什么。
请帮忙。
以下是更多详细信息:
OS: 64bit Windows 8.1 on Intel x64 (i3) Architecture of Compiler: MinGW64 G++ Compiler
这就是你的做法:
#include <iostream>
#include <string.h>
using namespace std;
class Employee
{
string name;
int age;
int salary;
public:
Employee(int agex, string namex, int salaryx)
{
name = namex;
age = agex;
salary = salaryx;
}
int getSalary()
{
return salary;
}
int getAge()
{
return age;
}
string getName()
{
return name;
}
};
int main(void)
{
//Create an Array length of 10
// in c++ its static you have to give the length of the array while declaring
Employee mycompany [10];
//Create a new Object
mycompany[0] = new Employee(10, "Mayukh", 1000);
string name = mycompany[0]->getName();
cout << name << "\n";
return 0;
}
一如既往,这里的建议是使用一些 STL 容器,例如 std::vector
。
也就是说你可能需要一个默认的 Employee
构造函数(除非你总是使用已有的构造函数初始化 所有 容器的元素,如果您要手动分配内存,您不太可能会这样做,但如果您使用 std::vector
).
则更有可能
//...
Employee() = default; // or Employee(){}
Employee(int agex, string namex, int salaryx)
{
name = namex;
age = agex;
salary = salaryx;
}
//...
如果你真的,绝对,必须通过手动内存分配来完成,它看起来大致是这样的:
// Employee array with 5 employees, with the first two initialized with your constructor
Employee *mycompany = new Employee[5] {{10, "Mayukh1", 1000}, {20, "Mayukh2", 2000}};
//adding an employee
mycompany[2] = {30, "Mayukh3", 3000};
// it still has space for 2 more
之后别忘了删除记忆:
delete [] mycompany;
我正在尝试用 C++ 创建一个对象数组。由于 C++ 支持 int、float 等原生对象,因此创建它们的数组不是问题。
但是当我创建一个 class 并创建该 class 的对象数组时,它不起作用。
这是我的代码:
#include <iostream>
#include <string.h>
using namespace std;
class Employee
{
string name;
int age;
int salary;
public:
Employee(int agex, string namex, int salaryx)
{
name = namex;
age = agex;
salary = salaryx;
}
int getSalary()
{
return salary;
}
int getAge()
{
return age;
}
string getName()
{
return name;
}
};
int main(void)
{
Employee **mycompany = {};
//Create a new Object
mycompany[0] = new Employee(10, "Mayukh", 1000);
string name = mycompany[0]->getName();
cout << name << "\n";
return 0;
}
没有编译错误,但是当我运行程序时,它崩溃了。我不知道这里到底发生了什么。
请帮忙。
以下是更多详细信息:
OS: 64bit Windows 8.1 on Intel x64 (i3) Architecture of Compiler: MinGW64 G++ Compiler
这就是你的做法:
#include <iostream>
#include <string.h>
using namespace std;
class Employee
{
string name;
int age;
int salary;
public:
Employee(int agex, string namex, int salaryx)
{
name = namex;
age = agex;
salary = salaryx;
}
int getSalary()
{
return salary;
}
int getAge()
{
return age;
}
string getName()
{
return name;
}
};
int main(void)
{
//Create an Array length of 10
// in c++ its static you have to give the length of the array while declaring
Employee mycompany [10];
//Create a new Object
mycompany[0] = new Employee(10, "Mayukh", 1000);
string name = mycompany[0]->getName();
cout << name << "\n";
return 0;
}
一如既往,这里的建议是使用一些 STL 容器,例如 std::vector
。
也就是说你可能需要一个默认的 Employee
构造函数(除非你总是使用已有的构造函数初始化 所有 容器的元素,如果您要手动分配内存,您不太可能会这样做,但如果您使用 std::vector
).
//...
Employee() = default; // or Employee(){}
Employee(int agex, string namex, int salaryx)
{
name = namex;
age = agex;
salary = salaryx;
}
//...
如果你真的,绝对,必须通过手动内存分配来完成,它看起来大致是这样的:
// Employee array with 5 employees, with the first two initialized with your constructor
Employee *mycompany = new Employee[5] {{10, "Mayukh1", 1000}, {20, "Mayukh2", 2000}};
//adding an employee
mycompany[2] = {30, "Mayukh3", 3000};
// it still has space for 2 more
之后别忘了删除记忆:
delete [] mycompany;