Simple: Operator overloading with heap objects
Okay, it's seems like I'm missing something here:
Code:
#include <QString>
#include <iostream>
class TestItem
{
public:
{
m_value = value;
}
{
return m_value;
}
bool operator==(const TestItem &other) const
{
return this->value() == other.value();
}
private:
};
int main(int argc, char *argv[])
{
TestItem *one = new TestItem("abc");
TestItem *two = new TestItem("abc");
if(one == two)
std::cout << "Match";
else
std::cout << "No Match";
return 0;
}
This returns ALWAYS "No Match". But if I create the objects on the stack and not on the heap it works:
Code:
TestItem one("abc");
TestItem two("abc");
What do I have to do to make it work with heap initialized objects :o ? Thank you.
Re: Simple: Operator overloading with heap objects
You are comparing pointers instead of the objects itself, try:
Code:
if (*one == *two)
...
Re: Simple: Operator overloading with heap objects
Yeah, I know, thanks :)
I just thought there might be a way to do it without the stars?
Re: Simple: Operator overloading with heap objects
This should work:
Code:
bool operator==(const TestItem *one, const TestItem *two) const {
return one->value() == two->value();
}
But this is risky, because you might have trouble checking for null values.
Re: Simple: Operator overloading with heap objects
Quote:
Originally Posted by
wysota
This should work:
Code:
bool operator==(const TestItem *one, const TestItem *two) const {
return one->value() == two->value();
}
But this is risky, because you might have trouble checking for null values.
This one is probably safer, though a little slower...
Code:
bool operator==(const TestItem *one, const TestItem *two) const {
return (one && two) ? (one->value() == two->value()) : false;
}
Re: Simple: Operator overloading with heap objects
Sure nothing is stopping you from doing this.
But you probably wouldn't want to do this kind of overloading as you are giving a new meaning to the == operator on pointers. Many a times the users of your code would not think that you are comparing the values.
Re: Simple: Operator overloading with heap objects
Quote:
Originally Posted by
sunil.thaha
But you probably wouldn't want to do this kind of overloading as you are giving a new meaning to the == operator on pointers. Many a times the users of your code would not think that you are comparing the values.
That's why it's impossible in C++ ;)
Re: Simple: Operator overloading with heap objects
Quote:
That's why it's impossible in C++ ;)
You are absolutely right. And thank you spotting out our mistake. I will never forget this :)
BTW: did you try out what was said here. Or you got it at the first go ?
Re: Simple: Operator overloading with heap objects
Quote:
Originally Posted by
sunil.thaha
did you try out what was said here. Or you got it at the first go ?
I knew that it's impossible, but I've checked it just to be sure.
Re: Simple: Operator overloading with heap objects
Quote:
Originally Posted by
jacek
I knew that it's impossible, but I've checked it just to be sure.
Just curious : what error was thrown by the compiler??? (don't tell me "check it on your own" because I just can't ATM...)
Re: Simple: Operator overloading with heap objects
Quote:
Originally Posted by
fullmetalcoder
Just curious : what error was thrown by the compiler???
Something like: "at least one of operator arguments must be a class or an enum".
Re: Simple: Operator overloading with heap objects
I wonder if the behaviour is simmilar for all popular compilers.
Re: Simple: Operator overloading with heap objects
Quote:
Originally Posted by
wysota
I wonder if the behaviour is simmilar for all popular compilers.
It should be, at least if they are C++ compilers:
Quote:
An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.