Overloading ==?

Started by
9 comments, last by Taralieth 18 years ago
I'm not sure if I'm doing this right:

#include <string>

class CStudent
{
public:

	CStudent( std::string sName, int nClass, int nHouse )
        {
        	this->m_sName = sName;
	        this->m_nClass = nClass;
	        this->m_nHouse = nHouse;
        }

	std::string	m_sName;
	
	int m_nClass;
	int	m_nHouse;

	CStudent* pOpponent1;
	CStudent* pOpponent2;
	CStudent* pOpponent3;
	CStudent* pOpponent4;

	inline bool operator == (CStudent sOpponent)
	{
		if( this->m_sName == sOpponent.m_sName )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

But trying this doesn't work:

        CStudent* one;
	CStudent* two;

	one = new CStudent( "Name", 1, 2 );
	two = new CStudent( "Name", 1, 2 );

	if( one == two )
	{
		MessageBox( NULL, "Equal", "", MB_OK);
	}

What am I doing wrong when overloading ==? It is a bool right?
Advertisement
You are comparing the locations the two pointers point at rather than what is at those locations. Also, you have a memory leak, unless you delete one and two later in the code.
I know about the leak, I only wrote that as a quick test.

So how should I pass them to ==?
if( *one == *two )

Or write a free function for pointers.


jfl.
Quote:Original post by Smit
I know about the leak, I only wrote that as a quick test.

So how should I pass them to ==?


Since you are using pointers, try derefencing them first. Like this:
if (*one == *two) ...


EDIT: Bah, too slow.
As Roboguy said you are comparing pointers. To compare what they point at you need to dereference them:

if (*one == *two)


You could also just create the objects on the stack.

CStundent one("Name", 1, 2);
CStundent two("Name", 1, 2);
if (one == two)

A secondary problem is that the the overloaded == is not defined properly, it's not causing a problem here, but this is how it should be declared:
inline bool operator == (const CStudent& sOpponent) const
{
}

Note that the 'inline' is redundant, you could remove it.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
And you can simplify it to:

bool operator == ( const CStudent &sOpponent ) const {  return this->m_sName == sOpponent.m_sName;}
Try this.

#include <string>class CStudent{public:	CStudent(std::string sName, int nClass, int nHouse)	{		sName = sName;		m_nClass = nClass;		m_nHouse = nHouse;	}	std::string m_sName;	int m_nClass;	int m_nHouse;	CStudent *pOpponent1;	CStudent *pOpponent2;	CStudent *pOpponent3;	CStudent *pOpponent4;	inline bool operator==(const CStudent &sOpponent) const	{		return (m_sName == sOpponent.m_sName)	}};


by using the expression itself as the return value it makes for more concise code. sOpponent is passed using a reference and const. Passing by reference (that is the & symbol) means that you actually need an object, but when you compare the two values (this is shown in the next bit of code) that a new object is not constructed and destructed. const shows that the object is non-modifiable in the context of the function. The second const shows that you dont modify the object refered to as *this .

Now about calling your equality operator, try this:

CStudent *one = new CStudent("Name", 1, 2);CStudent *two = new CStudent("Name", 1, 2);if(*one == *two) // this line is important{	MessageBox(NULL, "Equal", 0, MB_OK);}delete one;delete two;


This will work because you are comparing the objects not the pointers to the objects. new returns a pointer, not an object. That is you did wrong before, just correct the marked line. The other info I wrote is just better form.

EDIT: I'll remember not to write such a long response next time.
Quote:Original post by ZedFx
EDIT: I'll remember not to write such a long response next time.
It's a pretty good summary, though.
Quote:Original post by Shannon Barber
You could also just create the objects on the stack.

CStundent one("Name", 1, 2);
CStundent two("Name", 1, 2);


Quoted for emphasis. In C++, think before you 'new'. There are a lot of rules of thumb that go "Use X when you can; use pointers when you have to" - for good reason.

This topic is closed to new replies.

Advertisement