!= 运算符重载的参数问题

发布于 2024-09-28 22:52:31 字数 1345 浏览 4 评论 0原文

我正在尝试为 != 运算符定义重载。我的代码如下。 (更新:过时的代码。如果两个文章指针中的一个指向 NULL,此代码将崩溃。)

bool ArticleContainer::ArticleIterator::operator!=(const ArticleIterator& artit) {
    if (this->article == NULL && artit.article == NULL)
        return false;
    else if (this->article->GetID() != artit.article->GetID())
        return true;
    else if (this->article->GetName() != artit.article->GetName())
        return true;
    else
        return false;
}

当我在其第一行代码上放置断点时,我在调试器中看到了这一点。

这个 - 0x22fedc

artit - 无法创建变量对象

显然该函数无法访问artit,因此它崩溃了。我做错了什么?

编辑:调用发生在此处。

for (ArticleContainer::ArticleIterator art = cont.Begin(); art != cont.End(); art++) {
    cout << art << "\n";
}

基本上,我会浏览一系列文章,直到遇到哨兵。

我刚刚在 for 循环之前测试了 cont.End()

const ArticleIterator& End() const { return *tail; }

With tail:

Name : tail
    Details:0x571900

Edit:operator++ 代码如下:

void ArticleContainer::ArticleIterator::operator++(int i) {
    this->article = this->next->article;
    this->next = this->next->next;
}

I'm trying to define an overload for the != operator. My code is as follows. (Update: outdated code. If one of two article pointers points to NULL, this code will crash.)

bool ArticleContainer::ArticleIterator::operator!=(const ArticleIterator& artit) {
    if (this->article == NULL && artit.article == NULL)
        return false;
    else if (this->article->GetID() != artit.article->GetID())
        return true;
    else if (this->article->GetName() != artit.article->GetName())
        return true;
    else
        return false;
}

When I put a breakpoint on its first line of code, I saw this in the debugger.

this - 0x22fedc

artit - Unable to create variable object

Apparently the function can't access artit, so it crashes. What am I doing wrong?

Edit: the call happens here.

for (ArticleContainer::ArticleIterator art = cont.Begin(); art != cont.End(); art++) {
    cout << art << "\n";
}

Basically, I walk through a list of articles until I encounter the sentinel.

I just tested cont.End() right before the for loop:

const ArticleIterator& End() const { return *tail; }

With tail:

Name : tail
    Details:0x571900

Edit: operator++ code is as follows:

void ArticleContainer::ArticleIterator::operator++(int i) {
    this->article = this->next->article;
    this->next = this->next->next;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咋地 2024-10-05 22:52:31

我认为您的代码的意图是错误的,但从技术上讲,您可以尝试这样做:

bool ArticleContainer::ArticleIterator::operator!=(const ArticleIterator& artit) {
    if (article == NULL && artit.article == NULL)
        return false;
    if (article == NULL || artit.article == NULL)
        return true;
    if (article->GetID() != artit.article->GetID())
        return true;
    if (article->GetName() != artit.article->GetName())
        return true;
    return false;
}

但是,即使只考虑技术,我也宁愿用operator!=来表达operator!= 运算符==

干杯&呵呵,,

I think the intent of your code is wrong, but technically you can try this:

bool ArticleContainer::ArticleIterator::operator!=(const ArticleIterator& artit) {
    if (article == NULL && artit.article == NULL)
        return false;
    if (article == NULL || artit.article == NULL)
        return true;
    if (article->GetID() != artit.article->GetID())
        return true;
    if (article->GetName() != artit.article->GetName())
        return true;
    return false;
}

However, even considering only the technical, I'd rather express operator!= in terms of operator==.

Cheers & hth.,

奈何桥上唱咆哮 2024-10-05 22:52:31

bool ArticleContainer::ArticleIterator::operator!=(const ArticleContainer::ArticleIterator& artit);

bool ArticleContainer::ArticleIterator::operator!=(const ArticleContainer::ArticleIterator& artit);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文