views:

249

answers:

4

I am facing some problem with use of operator == in the following c++ program.

#include < iostream>
using namespace std;

class A
{
    public:
        A(char *b)
        {
            a = b;
        }
        A(A &c)
        {
            a = c.a;
        }
        bool operator ==(A &other)
        {
            return strcmp(a, other.a);
        }
    private:
        char *a;
};


int main()
{
    A obj("test");
    A obj1("test1");

    if(obj1 == A("test1"))
    {
        cout<<"This is true"<<endl;
    }
}

What's wrong with if(obj1 == A("test1")) line ?? Any help is appreciated.

+3  A: 
bool operator ==( const A &other)

Use const reference, so a temporary object that is constructed in if statement can be used as parameter for operator==.

Cătălin Pitiș
This is the wrong answer. The strcmp problem is correct.
Graeme Perrow
CPPDev
can you clarity the answer with example ??
Ashish
@Graeme Perrow: It's not wrong. There *additionally* is a problem with `strcmp`, but without the change here in this answer it won't even compile.
sth
@Graeme: It is the right answer. I quote the question: "What's wrong with if(obj1 == A("test1")) line ?". There is also the problem with strcmp, but it wasn't that the question...
Cătălin Pitiș
The OP was expecting to see "This is true" as the output - the original code did not print it. I copied the original code to a file to verify this. (@sth: It compiled successfully.) I added the `const` lines and rebuilt it, and it __still printed nothing__, meaning that your answer did not fix the problem. If I made the strcmp change in Mike Weller's answer, the output was correct, with or without the `const` additions.
Graeme Perrow
Steve Jessop
Oh, or could it be a C++0x thing?
Steve Jessop
I'm using MS Visual Studio 2005 on Windows. I tried it on a Linux box and there I _do_ see the compile error. So depending on the platform, this answer is not _wrong_, just incomplete. Mike's `strcmp` change is still required for correct behaviour.
Graeme Perrow
+34  A: 

strcmp returns 0 when the strings are equal, so you want:

return strcmp(a, other.a) == 0;

You should also use a const reference like Cătălin Pitiș says in his answer, because then you can use temporary objects with the operator, and you should also make the method itself const (since it doesn't modify the object) as Andreas Brinck says in the comments below. So your method should be:

bool operator ==(const A &other) const
{
        return strcmp(a, other.a) == 0;
}
Mike Weller
Andreas Brinck
+1  A: 

It looks to me like you want this in your operator:

strcmp(a, other.a) == 0

strcmp returns 0 when strings match, and a number indicating whether the comparison is greater or less than otherwise.

Simon Steele
A: 

Your error is that you create an instant value and pass it as reference to the operator== method. But your error is in your operator definition that should be:

bool operator==(const A& other) const

the body being the same.

PierreBdR
Adding the const stuff is definitely a good idea, _just_ adding the consts with "the body being the same" results in the same incorrect behaviour as the original program.
Graeme Perrow
Correction: on some platforms, the const stuff is more than just a good idea, it's required. Mike's `strcmp` change is still required for correct behaviour.
Graeme Perrow