将Class对象存储在数组中

时间:2022-12-30 20:21:44

Please take a look at the following:

请看一下以下内容:

dispenserType type[4];
dispenserType appleJuice();
type[0] = appleJuice;
dispenserType orangeJuice();
type[1] = orangeJuice;
dispenserType mangoLassi();
type[2] = mangoLassi;
dispenserType fruitPunch();
type[3] = fruitPunch;

as you can see i have stored different objects in the type array.

正如您所看到的,我在类型数组中存储了不同的对象。

The dispenserType class has a constructor that sets the number of items to 20. so we have int numberfItems for the object appleJuice set to 20 so on and so forth.

dispenserType类有一个构造函数,它将项目数设置为20.因此我们将对象appleJuice的int numberfItem设置为20,依此类推。

The same class has also the following methods: method that decreases the numberOfItems by one:

同一个类还有以下方法:将numberOfItems减少一个的方法:

void dispenserType::makeSale()
{
    numberOfItems--;
}
int dispenserType::getNoOfItems() const
{
    return numberOfItems;
}

If for example the following is called:

例如,如果调用以下内容:

appleJuice.makeSales
cout << appleJuice.getNoOfItems() << endl;

then the program would display the right numberOfItmes, which is 19. On the other hand, if cout<<type[0].getNoOfItems() is called, then it would display the number 20.

然后程序将显示正确的numberOfItmes,即19。另一方面,如果调用cout << type [0] .getNoOfItems(),那么它将显示数字20。

That behavior leads me to think that appleJuice and type[0] are two separate objects.

这种行为让我认为appleJuice和type [0]是两个独立的对象。

My questions is, is there a way to create an array of objects without needing to declare an object and store in an array as I did it as I did?

我的问题是,有没有办法创建一个对象数组而不需要像我一样声明一个对象并存储在一个数组中?

thank you.

谢谢。

2 个解决方案

#1


0  

You are correct, type[0] and appleJuice are two different objects.

你是对的,键入[0],appleJuice是两个不同的对象。

Option #1 - You can use an array of objects without cloning each object:

选项#1 - 您可以使用一组对象而无需克隆每个对象:

dispenserType type[4];

Option #2 - You can use an array of pointers instead of an array of objects:

选项#2 - 您可以使用指针数组而不是对象数组:

dispenserType* type[4];
for (int i=0; i<sizeof(type)/sizeof(*type); i++)
    type[i] = new dispenserType();

And of course, you will have to delete each allocated object at some later point during execution:

当然,您必须在执行期间稍后删除每个已分配的对象:

for (int i=0; i<sizeof(type)/sizeof(*type); i++)
    delete type[i];

#2


1  

You are not storing any objects, because you aren't creating any. This is a function declaration:

您没有存储任何对象,因为您没有创建任何对象。这是一个函数声明:

dispenserType appleJuice();

You need

你需要

dispenserType appleJuice;

or

要么

dispenserType appleJuice{}; // C++11

Once you fix that, then yes, doing this

一旦你修复了,那么是的,这样做

type[0] = appleJuice;

assigns the value of appleJuice to an already existing object type[0]. It is the same as saying

将appleJuice的值赋给已存在的对象类型[0]。这跟说的一样

dispenserType a;
dispenserType b;

b = a;  // b and a are different objects

My questions is, is there a way to create an array of objects without needing to declare an object and store in an array as I did it as I did?

我的问题是,有没有办法创建一个对象数组而不需要像我一样声明一个对象并存储在一个数组中?

For a plain array, the only way is to use an initialization list, because once it is initialized, all you can do is modify its existing elements. So

对于普通数组,唯一的方法是使用初始化列表,因为一旦初始化,您所能做的就是修改其现有元素。所以

dispenserType type[3] = { dispenserType(args), 
                          dispenserType(other args), 
                          dispenserType(other args) };

An alternative is to construct an std::vector<dispenserType> and emplace_back elements into it.

另一种方法是在其中构造一个std :: vector 和emplace_back元素。

#1


0  

You are correct, type[0] and appleJuice are two different objects.

你是对的,键入[0],appleJuice是两个不同的对象。

Option #1 - You can use an array of objects without cloning each object:

选项#1 - 您可以使用一组对象而无需克隆每个对象:

dispenserType type[4];

Option #2 - You can use an array of pointers instead of an array of objects:

选项#2 - 您可以使用指针数组而不是对象数组:

dispenserType* type[4];
for (int i=0; i<sizeof(type)/sizeof(*type); i++)
    type[i] = new dispenserType();

And of course, you will have to delete each allocated object at some later point during execution:

当然,您必须在执行期间稍后删除每个已分配的对象:

for (int i=0; i<sizeof(type)/sizeof(*type); i++)
    delete type[i];

#2


1  

You are not storing any objects, because you aren't creating any. This is a function declaration:

您没有存储任何对象,因为您没有创建任何对象。这是一个函数声明:

dispenserType appleJuice();

You need

你需要

dispenserType appleJuice;

or

要么

dispenserType appleJuice{}; // C++11

Once you fix that, then yes, doing this

一旦你修复了,那么是的,这样做

type[0] = appleJuice;

assigns the value of appleJuice to an already existing object type[0]. It is the same as saying

将appleJuice的值赋给已存在的对象类型[0]。这跟说的一样

dispenserType a;
dispenserType b;

b = a;  // b and a are different objects

My questions is, is there a way to create an array of objects without needing to declare an object and store in an array as I did it as I did?

我的问题是,有没有办法创建一个对象数组而不需要像我一样声明一个对象并存储在一个数组中?

For a plain array, the only way is to use an initialization list, because once it is initialized, all you can do is modify its existing elements. So

对于普通数组,唯一的方法是使用初始化列表,因为一旦初始化,您所能做的就是修改其现有元素。所以

dispenserType type[3] = { dispenserType(args), 
                          dispenserType(other args), 
                          dispenserType(other args) };

An alternative is to construct an std::vector<dispenserType> and emplace_back elements into it.

另一种方法是在其中构造一个std :: vector 和emplace_back元素。