类模板 - 从"box"对象中删除项
Class templates - Removing an item from a "box" object
我把要点贴在这里:
这是一门数据结构课程。我们正在创建一个简单的PlainBox类并创建成员函数,这些成员函数允许我们从"box"中添加和删除项,检查框是否为空,以及检查私有数据成员的值。我已经完成了作业中的所有内容,除了一件事:我不确定如何创建remove()函数,以便我们可以从已经包含内容的框中删除项。
下面是说明:"向模板类添加一个名为"remove"的公共方法。此方法将没有参数并返回bool值。如果盒子中有一个项目,该项目应该被删除(使盒子为空),该方法应该返回true(任务完成)。否则,不修改框并返回false(任务无法执行)。"
remove()函数在PlainBox.cpp文件中。我只是困惑如何重置"ItemType项目"变量为默认值,使框为空。我应该把'item'设置为空字符串吗?(item = " ";)
template<class ItemType>
bool PlainBox<ItemType>::remove()
{
if (full == true)
{
full = false;
return true;
}
else
{
return false;
}
}
在main.cpp文件的底部,我正在测试secondNumberBox上的remove()函数,然后检查私有成员变量是否为空。
下面是供参考的PlainBox类:// Declaration for the class PlainBox
class PlainBox: public BoxInterface<ItemType> // added parent class
{
private:
// Data field
ItemType item;
bool full;
public:
// Default constructor
PlainBox();
// Parameterized constructor
PlainBox(const ItemType& theItem);
// Accessor method to get the value of the data field
ItemType getItem() const;
// Add method
bool add(const ItemType& theItem);
// Remove method
bool remove();
// isEmpty method
bool isEmpty();
}; // end PlainBox
我希望我这篇文章的措辞正确,如果这篇文章没有任何意义,请告诉我!
为什么要弄乱item?您有一个标志(bool full),表示是否使用项目。我相信你有一个方法,写到项目时,标志是假的。如果bool full为假,那么写入方法是否关心item中的内容?
相关文章:
- 线程调用的函数对对象删除是否安全?
- 当我使用dynamic_cast并删除对象删除时,析构函数是如何工作的?
- 使用 com 对象删除计划任务
- C 将对象删除为参考
- C样式对象删除的包装器
- 静态映射中动态分配的对象.删除必要的
- C++指向对象删除的指针数组,"Debug assertion failure!"
- 在其成员函数返回之前,将对象删除是不确定的行为吗?
- Python 对象删除检测
- C++安全对象删除
- Qt:RemoveWidget和对象删除
- Qt对象删除导致崩溃
- 删除由两个shared_pointer实例管理的对象(删除其中一个)
- 顶点缓冲区对象(删除进程)opengl
- 设计 -- 按名称或对象删除
- 提升中的异常:进程间,共享内存对象删除
- 使用std::function和std::bind来存储回调并处理对象删除
- 如何使用智能指针防止双重对象删除
- 指针指向对象删除时自动为空
- 对象层次结构中的对象删除责任