gpt4 book ai didi

c++ - 如何覆盖运算符 <

转载 作者:搜寻专家 更新时间:2023-10-31 01:21:31 25 4
gpt4 key购买 nike

我正在尝试重写运算符 <,如下所示:

内部节点:

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

车内:

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

调用操作符:

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

vector 定义:

vector<Node<T>*> m_heapVector;

我检查了调用,它没有调用覆盖的运算符。

最佳答案

这是因为你在比较指针,

你必须做到:

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

并相应地调整运算符

bool operator<(const Node &other) const;

关于c++ - 如何覆盖运算符 <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3746462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com