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.
virtual. (Dumb IDE's forcing bad practice. If I need a destructor I'll write it my damn self.)newto create an instance of a variable. This is a technique that is used in Java. Just declare your variables asTestClass tc; TestClass tc1;. Until you get experience, only usenewfor huge objects; pass everything by reference orconstreference.