我有一个名为 Particle 的类,它有一个 std::set 作为成员。该类如下所示:
class Particle {
private:
std::set<vtkIdType> cells;
std::set<vtkIdType>::iterator ipc;
public:
Particle() {};
enum state {EXISTS = -1, SUCCESS = 0, ERROR = 1};
state addCell(const vtkIdType cell);
int numCells() { return static_cast<int>(cells.size()); }
vtkIdType getFirstCell() { return (*(ipc = this->cells.begin()));}
vtkIdType getNextCell() { return *(++ipc); }
vtkIdType hasNextCell() { ++ipc; if (ipc == this->cells.end()) return false; --ipc; return true; }
std::string getOutput();
};
我对 非常不满意getFirstCell()
,getNextCell()
尤其是hasNextCell()
它们的存在是因为我不想暴露集合本身。我不得不使用这种方式++ipc
,--ipc
因为if((ipc+1) == this->cells.end())
给出了编译器错误,ipc+1 似乎是问题所在。
封装一个集合并访问它的好方法是什么?另外,有没有摆脱这个getFirstCell()
功能的好方法?
提前致谢。
编辑:我发布的代码只是类结构的一个例子。“真实”类包含更多的集合和其他对这个问题不那么重要的数据(我假设)。