Thread: Basic operator overloading question

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Basic operator overloading question

    Hi there,

    I'm just starting to try out Operator Overloading and wrote a basic definition for the equality operator ==.

    base.h :
    Code:
    class Base
    {
    public:
    	Base (int a);
    	int getNo() const { return m_a; }
    
    private:
    	int m_a;
    };
    Base::Base(int a)
    {
    	m_a = a;
    
    }
    bool operator==(const Base& leftparameter, const  Base& rightparameter)
    {
    	leftparameter.getNo() == rightparameter.getNo();
    	return true;
    }

    main.cpp :

    Code:
    #include <iostream>
    #include "base.h"
    
    using namespace std;
    
    int main()
    {
    	Base obj1(1);
    	Base obj2(1);
    
    	if(obj1 == obj2)
    	{
    		cout << "Objects match" << endl;
    	}
    	else
    	{
    		cout << "Objects don't match" << endl;
    	}
    	system ("pause");
    	return 0;
    }
    Just a basic query, Visual Studio reports:

    warning C4553: '==' : operator has no effect; did you intend '='?
    I'm just wondering why it says == has no effect when it does successfully compare the two objects.
    I don't see why would it think I meant to assign one object to another.

    Should I not worry about it? Or am I missing the point of the warning?

    Thanks for any info!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swerve
    I'm just wondering why it says == has no effect when it does successfully compare the two objects.
    You probably want to return the result of that comparison. The problem at the moment is that you compare, but you discard the results of the comparison and return true. Therefore, we can remove that comparison, and the program will still appear to behave exactly the same (other than running a little faster).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It successfully compares the two objects, but you ignore the result of the comparison.

    The warning thinks you made a different mistake, but there definitely is something you need to fix.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Ahh Ok.

    Code:
    bool operator==(const Base& leftparameter, const  Base& rightparameter)
    {
    	if(leftparameter.getNo() == rightparameter.getNo())
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    Now I'm getting no warnings, and I'm actually using the results.

    Seems to work fine now.

    Thanks guys, just wanted to make sure I was getting the fundamentals right.


  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indeed, but you could have written:
    Code:
    bool operator==(const Base& leftparameter, const  Base& rightparameter)
    {
    	return leftparameter.getNo() == rightparameter.getNo();
    }
    Incidentally, lhs and rhs are sometimes used as abbreviations for "left hand side" and "right hand side" respectively for such situations as these.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks laserlight!

    Great tips!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM