Basic operator overloading question

This is a discussion on Basic operator overloading question within the C++ Programming forums, part of the General Programming Boards category; Hi there, I'm just starting to try out Operator Overloading and wrote a basic definition for the equality operator ==. ...

  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
    21,436
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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
    21,436
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21