努力获得'=='运算符重载工作 (C++)

发布于 2024-09-15 16:44:59 字数 1240 浏览 2 评论 0原文

好吧,我不确定我在这里做什么,除了这是不对的。试图重载类的“==”方法,但它只是......不起作用。至少,我从我的 main 得到了一个错误的返回,并且 '==' 实现中的 cout 没有输出。

这是我的三个文件:

// TestClass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

class TestClass {
public:
    TestClass(int contents);
    TestClass(const TestClass& orig);
    virtual ~TestClass();
    bool operator==(const TestClass& other);
private:
    int contents;
};

#endif  /* TESTCLASS_H */



// TestClass.cpp

#include <iostream>

#include "TestClass.h"

TestClass::TestClass(int contents) {
    this->contents = contents;
}

TestClass::TestClass(const TestClass& orig) {
    this->contents = orig.contents;
}

TestClass::~TestClass() {
}

bool TestClass::operator ==(const TestClass& other) {
    std::cout << "COMPARING" << std::endl;
    return (contents == other.contents);
}


// Main.cpp

#include <cstdlib>
#include <iostream>

#include "TestClass.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    TestClass* tc = new TestClass(1);
    TestClass* tc1 = new TestClass(1);

    cout << (tc == tc1) << endl;

    return 0;
}

所以问题是 - 我做错了什么?我为某个地方可能犯了一个非常愚蠢的错误而道歉,但我就是无法发现它。

Okay, not sure what I'm doing here, other than it's not right. Trying to overload the '==' method of a class, and it's just... not working. At least, I get a false back from my main, and the cout in the implementation of '==' doesnt output.

These are my three files:

// TestClass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

class TestClass {
public:
    TestClass(int contents);
    TestClass(const TestClass& orig);
    virtual ~TestClass();
    bool operator==(const TestClass& other);
private:
    int contents;
};

#endif  /* TESTCLASS_H */



// TestClass.cpp

#include <iostream>

#include "TestClass.h"

TestClass::TestClass(int contents) {
    this->contents = contents;
}

TestClass::TestClass(const TestClass& orig) {
    this->contents = orig.contents;
}

TestClass::~TestClass() {
}

bool TestClass::operator ==(const TestClass& other) {
    std::cout << "COMPARING" << std::endl;
    return (contents == other.contents);
}


// Main.cpp

#include <cstdlib>
#include <iostream>

#include "TestClass.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    TestClass* tc = new TestClass(1);
    TestClass* tc1 = new TestClass(1);

    cout << (tc == tc1) << endl;

    return 0;
}

So the question is - what have I done wrong? I apologise for what is probably a very silly mistake somewhere, but I just can't spot it.

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

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

发布评论

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

评论(2

長街聽風 2024-09-22 16:44:59

tc == tc1 比较指针值。它“应该”是 *tc == *tc1,但我不明白为什么你首先要动态分配。

自动(堆栈)分配是高度首选,仅当您需要对象独立于范围时才动态分配。 (然后使用自动分配的智能指针来跟踪它,这将在适当的时候删除指针。)


此外,运算符应该是 const,因为它不会修改 this code>:

//                                      vvvvv
bool operator==(const TestClass& other) const;

不过,更好的是一个免费函数:

bool operator==(const TestClass& lhs, const TestClass& rhs);

这可能是一个朋友。 (自由函数始终是首选,而且这允许 5 == tc 工作。)

tc == tc1 compares pointer values. It "should" be *tc == *tc1, but I don't get why you'd dynamically allocate in the first place.

Automatic (stack) allocation is highly preferred, only dynamically allocate when you need the object to be independent of scope. (And then keep track of it with automatically allocated smart pointers, which will delete the pointer when it's appropriate.)


Also, the operator should be const, because it doesn't modify this:

//                                      vvvvv
bool operator==(const TestClass& other) const;

Even better, though, is a free function:

bool operator==(const TestClass& lhs, const TestClass& rhs);

Which would possibly be a friend. (Free-functions are always preferred, plus this allows 5 == tc to work.)

赠我空喜 2024-09-22 16:44:59

您正在比较指针。尝试这样做:

cout << (*tc == *tc1) << endl;

两点注释:

  • 您应该释放分配的内存
    删除,或使用智能指针
  • 你应该声明operator==const

    bool operator==(const TestClass& other) const

You are comparing pointers. Try that instead:

cout << (*tc == *tc1) << endl;

Two remarks:

  • You should free allocated memory with
    delete, or use a smart pointer
  • You should declare operator== const:

    bool operator==(const TestClass& other) const

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