C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-20-2009, 12:04 PM   #1
Registered User
 
Join Date: Aug 2007
Location: U.K.
Posts: 147
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:

Quote:
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!
Swerve is offline   Reply With Quote
Old 11-20-2009, 12:20 PM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,315
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-20-2009, 12:20 PM   #3
Registered User
 
Join Date: Jan 2005
Posts: 7,251
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.
Daved is offline   Reply With Quote
Old 11-20-2009, 12:32 PM   #4
Registered User
 
Join Date: Aug 2007
Location: U.K.
Posts: 147
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.

Swerve is offline   Reply With Quote
Old 11-20-2009, 12:36 PM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,315
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-20-2009, 12:44 PM   #6
Registered User
 
Join Date: Aug 2007
Location: U.K.
Posts: 147
Thanks laserlight!

Great tips!

Swerve is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A basic math programming question hebali C Programming 38 02-25-2008 04:18 PM
Basic question about GSL ODE func RK4 cosmich Game Programming 1 05-07-2007 02:27 AM
Basic question about RK4 cosmich C++ Programming 0 05-07-2007 02:24 AM
A very basic question AshFooYoung C Programming 8 10-07-2001 03:37 PM


All times are GMT -6. The time now is 11:51 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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