[英]how to write nested Initialiser list, eg a QVector of QPairs
[英]How to intercept write operations on a QVector?
首先,第一件事:我知道从容器扩展是一种不好的做法,我也知道原因。 另一方面,我正在“播放已经处理过的代码”,没有时间适当地重构它。
我有一个类,我们称它为VectorOfSomethings
,它是从QVector<Something>
扩展而来的。 每个Something
都有一个hashCode()
方法,该方法唯一地标识它。
另一方面,还有许多其他类具有VectorOfSomethings
类型的成员,并且有很多operator == implemented
为m_Somethings == other.m_Somethings
。 这些运算符称为A LOT。
由于没有实际的方法来急切地计算散列,因此我不得不依靠VectorOfSomethings::hashCode()
方法,该方法遍历所有当前存储的Somethings,并根据它们的散列来计算集合的散列。点。 这是超级昂贵的。
拦截对VectorOfSomethings的所有写操作的最有效方法是什么,因此每次在其中添加/删除某项内容时,都将重新计算和缓存哈希值,以便在需要时为operator ==准备就绪?
尝试重载原始QVector方法。 非常优雅。 在某些情况下不起作用(这远比不雅致)。
这是代码现在的外观示意图:
class VectorOfSomething : public QVector<Something>, public SomeOtherNonEssentials
{
public:
// Insert other methods here
uint hashCode() const {
uint result = 0;
std::for_each(this->begin(), this->end(), [&result](Something value) {
result = regenerateHash(result, value.hashCode());
});
return result;
}
bool operator==(const VectorOfSomething &other) const
{
return hashCode() == other.hashCode();
}
}
不要从Qt容器继承(与STL容器一样,它们不旨在被继承,一个提示是它们没有虚拟析构函数)。
因此,使用composition并为内部向量提供所需的接口,这也将解决您的问题,它将拦截所有修改:
//Optional make it generic
class VectorOfSomething : public SomeOtherNonEssentials
{
public:
// Insert other methods here
// insert methods like insert/remove/add/push_back
void push_back(const Something& something)
{
//do whatever with something
m_internalVector.push_back(something);
}
private:
QVector<Something> m_internalVector;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.