努力获得'=='运算符重载工作 (C++)
好吧,我不确定我在这里做什么,除了这是不对的。试图重载类的“==”方法,但它只是......不起作用。至少,我从我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tc == tc1
比较指针值。它“应该”是*tc == *tc1
,但我不明白为什么你首先要动态分配。自动(堆栈)分配是高度首选,仅当您需要对象独立于范围时才动态分配。 (然后使用自动分配的智能指针来跟踪它,这将在适当的时候删除指针。)
此外,运算符应该是 const,因为它不会修改 this code>:
不过,更好的是一个免费函数:
这可能是一个朋友。 (自由函数始终是首选,而且这允许
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 modifythis
:Even better, though, is a free function:
Which would possibly be a friend. (Free-functions are always preferred, plus this allows
5 == tc
to work.)您正在比较指针。尝试这样做:
两点注释:
删除,或使用智能指针
你应该声明operator==const:
bool operator==(const TestClass& other) const
You are comparing pointers. Try that instead:
Two remarks:
delete, or use a smart pointer
You should declare operator== const:
bool operator==(const TestClass& other) const