This topic is 4341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
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.

Share on other sites
I know about the leak, I only wrote that as a quick test.

So how should I pass them to ==?

Share on other sites
if( *one == *two )

Or write a free function for pointers.

jfl.

Share on other sites
Quote:
 Original post by SmitI 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.

Share on other sites
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.

Share on other sites
And you can simplify it to:

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

Share on other sites
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 .

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.

Share on other sites
Quote:
 Original post by ZedFxEDIT: I'll remember not to write such a long response next time.
It's a pretty good summary, though.

Share on other sites
Quote:
 Original post by Shannon BarberYou 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.

Share on other sites
Indeed, consider declaring on the stack whenever possible. Not only is it less prone to memory leak errors, it is also faster to allocate variables on the stack compared to storing variables on the heap.