【发布时间】:2012-05-15 10:26:50
【问题描述】:
给定以下代码:
QList<Vertex*> _vertices; //this gets filled
//at some other point i want to check if there's already
//a vertex with the same payload inside the list
Vertex* v = new Vertex(payload);
int result = _vertices.indexOf(v);
if (result == -1){
//add the vertex to the list
} else {
//discard v and return pointer to match
}
//overloaded Vertex::operator==
bool Vertex::operator==(Vertex* other){
//i return true if my payload and the others
//payload are the same
}
据我所知,indexOf() 永远不会调用我的 operator==。我认为这是因为 QList 封装了一个指针类型和 indexOf() 比较指针。有没有办法在 QList 中保留 ponters 并且仍然使用我自己的 operator==()?
点赞Vertex*::operator==(Vertex* other)
相关问题:removing in pointer type Qlists | not working because of pointer type
编辑:意图。
两个顶点被认为是相等的。它们的payload携带的标识符是相等的。
Vertex 是Graph 类的一部分。我希望该类的客户能够调用Graph::addEdge(Payload,Payload) 来填充图表。然后图形对象负责将有效负载包装在顶点对象中并构建边。因此 Graph 需要检查封装给定有效负载的 Vertex 是否不存在。在编写代码时,使用 QList 似乎是“最简单的事情”。
【问题讨论】:
-
你不能存储 'QList
' 吗?重载 == 不适用于指针。而且,实际上你的代码是行不通的:你构造 new 并尝试在向量中查找它的 adress。不会成功!
标签: c++ qt operator-overloading qlist