0

So i've been told that in order for part of my program to work that I need to overload the == operator, unfortunately I have absolutely no idea how to do this, here is the class for which the operator needs to be overloaded

#ifndef PC_H
#define PC_H
#include <iostream>
#include <list>

using namespace std;

class PC{
protected:
    string name;
    string IP;
    int disk_size;
    string pass;
public:
    PC();
    PC(string, string, int, string);
    string getName();
    string getIP();
    int getSize();
    string getPass();
    void setName(string);
    void setIP(string);
    void setNewPass();          
    void getAllInfo();              
};

#endif

How would I go about overloading the == operator, thank you

Hunter Tipton
  • 313
  • 1
  • 4
  • 11
  • 2
    It doesn't get better than this http://stackoverflow.com/questions/4421706/operator-overloading – Support Ukraine Apr 24 '15 at 04:36
  • 2
    The mechanism for overloading == is the same as for any other operator. –  Apr 24 '15 at 04:36
  • Yes ive seen that page but it doesnt seem descriptive enough for me, for instance does "inline" or "bool" have to be replaced with anything representative of my class, and what goes in the space where it says "do actual comparison" – Hunter Tipton Apr 24 '15 at 04:38
  • No , `bool` is staying `bool`. You usually want to `inline` but you might don't use that. – Basile Starynkevitch Apr 24 '15 at 04:39
  • It sounds like your question is not "how do I overload `==`?" but "what does my instructor expect `==` to mean?" –  Apr 24 '15 at 04:39

1 Answers1

2

It's just a function. You overload it like any other function. You could use the following signature.

bool operator==(const PC& other) const;

And define it.

bool PC::operator==(const PC& other) const
{
    // Do comparisons of member state and return 'true' or 'false'
}

Alternatively you could make it a free function.

bool operator==(const PC& lhs, const PC& rhs)
{
    // Do comparisons of member state and return 'true' or 'false'
}
James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • ok but what would doing the comparison look like exactly? – Hunter Tipton Apr 24 '15 at 04:42
  • @HunterTipton It depends on what it means for them to be considered _equivalent_. For example, are they the same if just the name is the same? If so, then `return this->name == other.name` or `return lhs.getName() == rhs.getName()`. Otherwise, include any other necessary comparisons. It's up to you to implement the comparison in whatever way achieves the desired behavior. – James Adkison Apr 24 '15 at 04:45
  • ... in particular, you must know/decide what behavior is actually desired before you can implement `operator==`. –  Apr 24 '15 at 04:48