如何在C ++中构建一个对象数组,这些对象都继承自主类?

时间:2021-07-11 06:35:34

I am new to C++, and would like some help.

我是C ++的新手,想要一些帮助。

I need to create an array of objects, and from what I understood, it is done using :

我需要创建一个对象数组,根据我的理解,它是使用以下方法完成的:

MyObject* instance = new MyObject[nb_of_instances_in_array];

Though, it doesn't quite fit my needs.

虽然,它不太适合我的需要。

I have a main object from which inherits 3 other objects of varying methods and attributes.

我有一个主要对象,从中继承了不同方法和属性的3个其他对象。

mainObject <- Object1

mainObject < - Object1

mainObject <- Object2 <- Object3

mainObject < - Object2 < - Object3

Now I want to create an array that can contain Object1s, Object2s, and Object3s. I tried doing something like :

现在我想创建一个可以包含Object1s,Object2s和Object3s的数组。我尝试过这样的事情:

int nb_of_obj, i;
cin >> nb_of_obj;
mainObject* arrayOfMainObjects = new mainObject[nb_of_obj];
for(i = 0;i<nb_of_obj;i++)
{
//Menu to select Object to append to the array
 arrayOfMainObjects[i] = Object2(args...);\\there are between 3 and 5 args for each object
}
delete arrayOfMainObjects;

and I also tried a more C-oriented method using stdlib.h's malloc and free.

我还尝试了一个更加面向C的方法,使用stdlib.h的malloc和free。

using namespace std;

size_t max_size();
     return (size_t)(max(max(sizeof(Object1),sizeof(Object2)), max(sizeof(Object3), sizeof(mainObject))));

mainObject* arrayOfMainObjects = NULL;
arrayOfMainObjects = (mainObject*) malloc(nb_of_obj*max_size());
//Globally, same code than above
free(arrayOfMainObjects);

I don't know what I'm doing wrong, I even tried accessing the next "spot" in the array doing something like :

我不知道我做错了什么,我甚至尝试访问数组中的下一个“点”做类似的事情:

*(arrayOfMainObjects + i*max_size()) = Object2(args...);

instead of :

代替 :

arrayOfMainObjects[i] = Object2(args...);

2 个解决方案

#1


1  

Let's inspect why the following line doesn't work for you.

让我们检查为什么以下行不适合您。

mainObject* arrayOfMainObjects = new mainObject[nb_of_obj];

mainObject * arrayOfMainObjects = new mainObject [nb_of_obj];

Each element in arrayOfMainObject has the size sizeof(mainObject).

arrayOfMainObject中的每个元素的大小为sizeof(mainObject)。

Objects that inherit from mainObject will be larger than mainObject if the inherited object contains some extra data. For example sizeof(Object2)>=sizeof(mainObject) // always true!.

如果继承的对象包含一些额外数据,则从mainObject继承的对象将大于mainObject。例如sizeof(Object2)> = sizeof(mainObject)//总是如此!

So when you try to construct inherited Objects in the array, there is only space for sizeof(mainObject) and the rest get's sliced off.

因此,当您尝试在数组中构造继承的对象时,只有sizeof(mainObject)的空间,其余的被切掉。

To fix this, your array can consist of pointers to mainObject and you can cast the pointers to the type of the inherited object.

要解决此问题,您的数组可以包含指向mainObject的指针,您可以将指针强制转换为继承对象的类型。

But now, to avoid memory leak, each pointer in the array has to be freed when no longer used.

但是现在,为了避免内存泄漏,不再使用时必须释放数组中的每个指针。

You can use smart pointers and be free of the problem. Something like

您可以使用智能指针并解决问题。就像是

std::unique_ptr<mainObject>* arrayOfMainObjects = new std::unique_ptr<mainObject>[nb_of_obj];

std :: unique_ptr * arrayOfMainObjects = new std :: unique_ptr [nb_of_obj];

EDIT: Don't forget that delete arrayOfMainObjects is still needed.

编辑:不要忘记仍然需要删除arrayOfMainObjects。

#2


-1  

Just keep it simple:

保持简单:

class MyObject: public mainObject
{
}

MyObject* instance = new MyObject[nb_of_instances_in_array];

And everything will be easier.

一切都会变得更容易。

#1


1  

Let's inspect why the following line doesn't work for you.

让我们检查为什么以下行不适合您。

mainObject* arrayOfMainObjects = new mainObject[nb_of_obj];

mainObject * arrayOfMainObjects = new mainObject [nb_of_obj];

Each element in arrayOfMainObject has the size sizeof(mainObject).

arrayOfMainObject中的每个元素的大小为sizeof(mainObject)。

Objects that inherit from mainObject will be larger than mainObject if the inherited object contains some extra data. For example sizeof(Object2)>=sizeof(mainObject) // always true!.

如果继承的对象包含一些额外数据,则从mainObject继承的对象将大于mainObject。例如sizeof(Object2)> = sizeof(mainObject)//总是如此!

So when you try to construct inherited Objects in the array, there is only space for sizeof(mainObject) and the rest get's sliced off.

因此,当您尝试在数组中构造继承的对象时,只有sizeof(mainObject)的空间,其余的被切掉。

To fix this, your array can consist of pointers to mainObject and you can cast the pointers to the type of the inherited object.

要解决此问题,您的数组可以包含指向mainObject的指针,您可以将指针强制转换为继承对象的类型。

But now, to avoid memory leak, each pointer in the array has to be freed when no longer used.

但是现在,为了避免内存泄漏,不再使用时必须释放数组中的每个指针。

You can use smart pointers and be free of the problem. Something like

您可以使用智能指针并解决问题。就像是

std::unique_ptr<mainObject>* arrayOfMainObjects = new std::unique_ptr<mainObject>[nb_of_obj];

std :: unique_ptr * arrayOfMainObjects = new std :: unique_ptr [nb_of_obj];

EDIT: Don't forget that delete arrayOfMainObjects is still needed.

编辑:不要忘记仍然需要删除arrayOfMainObjects。

#2


-1  

Just keep it simple:

保持简单:

class MyObject: public mainObject
{
}

MyObject* instance = new MyObject[nb_of_instances_in_array];

And everything will be easier.

一切都会变得更容易。