开发者

how to Override operator <

开发者 https://www.devze.com 2023-01-16 14:43 出处:网络
I\'m trying to override operator < as the following : inside Node : bool operator <(const Node* other) {

I'm trying to override operator < as the following :

inside Node :

bool operator <(const Node* other) {
  return *(this->GetData()) < *(other->GetData());
}

inside vehicle :

bool operator <(const Vehicle &other) {
  return this->GetKilometersLeft() < other.GetKilometersLeft();
}

invoking the operator :

while (index > 0 && m_heapVector[index] < m_heapVector[parent(index)])

vector definition :

vector<Node<T>*> m_heapVector;

I c开发者_运维知识库hecked the call and it's not calling the overridden operators.


this is because you are comparing pointers,

You have to make it:

*m_heapVector[index] < *m_heapVector[parent(index)]

and adjust operator accordingly

bool operator<(const Node &other) const;
0

精彩评论

暂无评论...
验证码 换一张
取 消