C ++在数组中存储抽象对象 [英] C++ storing an abstract object in an array
问题描述
我一直试图通过引用传递一个抽象类的对象,但是当我尝试将该对象存储在具有相同数据类型的指针数组中时,它显然会产生错误。任何人可以解释我做错了什么?
它在以下函数中:
void enterItem(DessertItem& item)
{
if(top!=(maxSize-1))
{
arr [top] =& item;
top ++; sweetItem类实际上是一个抽象类,而arr 是一个甜点的指针数组,所以它可以引用任何新的对象,当它通过引用传递。我不能得到它,我应该怎么做?
以下是类: class Checkout
{
////保存DessertItem引用的列表。
protected:
int maxSize;
int top;
DessertItem ** arr;
public:
Checkout()
{
///创建一个Checkout实例DessertItem的空列表
this-> maxSize = 5;
this-> top = 0;
this-> arr = new DessertItem * [maxSize];
for(int i = 0; i {
arr [i] = NULL;
}
}
////清除Checkout以开始检查一组新项目
void clear()
{
for(int i = 0; i< maxSize; i ++)
{
arr [i] = NULL;
}
}
//将DessertItem添加到项目列表的末尾
void enterItem(DessertItem& item)
{
if(top!=(maxSize-1))
{
arr [top ] =& item;
top ++;
}
}
//返回列表中的DessertItem数量
int numberOfItems()
{
return this-> top;
}
};
解决方案将抽象对象存储在 vector< unique_ptr< T>>
。当你构造它们时,构造它们为 unique_ptr
,例如:
Base {
public:
virtual〜Base()= default;
virtual void methods()= 0;
};
类Impl:public Base {
public:
void method()override {/ * perform action here * /};
};
创建并存储如下:
//讨厌的老c风格数组
std :: unique_ptr< Base> a [10];
a [0] = std :: move(std :: unique_ptr< Base>(new Impl));
// std :: vector
std :: unique_ptr< Base> p {new Impl};
std :: vector< std :: unique_ptr< Base>> v;
v.push_back(std :: move(p));
//或
v.emplace_back(new Impl);
I have been trying to pass object of an abstract class as by reference but it clearly gives error when I try to store this objects in an array of pointers with the same data type. Can anyone please explain what I'm doing wrong?
It's in the following function:
void enterItem(DessertItem& item)
{
if (top != (maxSize-1))
{
arr[top] = &item;
top++;
}
}
The dessertItem class is actually an abstract class and "arr" is a pointer array of dessertItem so it can give reference to any new object whenever it gets passed by reference. I can't get it how am I supposed to do that?
Here's the class:
class Checkout
{
////Maintains a list of DessertItem references.
protected:
int maxSize;
int top;
DessertItem **arr;
public:
Checkout()
{
///Creates a Checkout instance with an empty list of DessertItem's
this->maxSize=5;
this->top=0;
this->arr = new DessertItem*[maxSize];
for(int i=0; i<maxSize; i++)
{
arr[i]=NULL;
}
}
////Clears the Checkout to begin checking out a new set of items
void clear()
{
for(int i=0; i<maxSize; i++)
{
arr[i]=NULL;
}
}
//A DessertItem is added to the end of the list of items
void enterItem(DessertItem &item)
{
if(top!=(maxSize-1))
{
arr[top]=&item;
top++;
}
}
//Returns the number of DessertItem's in the list
int numberOfItems()
{
return this->top;
}
};
解决方案 store your abstract objects in a vector<unique_ptr<T>>
. when you construct them, construct them as unique_ptr
, eg:
class Base {
public:
virtual ~Base() = default;
virtual void method() = 0;
};
class Impl : public Base {
public:
void method() override { /* perform action here */ };
};
create and store like so:
// nasty old c-style array
std::unique_ptr<Base> a[10];
a[0] = std::move(std::unique_ptr<Base>(new Impl));
// std::vector
std::unique_ptr<Base> p { new Impl };
std::vector<std::unique_ptr<Base>> v;
v.push_back(std::move(p));
// or
v.emplace_back(new Impl);
这篇关于C ++在数组中存储抽象对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!