4

i have a class which needs to implement the operator== and return type of it is bool. I'm not sure is below code the correct way to implement it.

//MyTest.hpp file

class Test { public: Test(); virtual ~Test(); int x,y; bool operator==(const Test &v); }; // MyTest.cpp file bool Test::operator==(const Test &v) { return (x== v.x) && (y==v.y); } 

even though the code compiles is this standard way to implement we need to use template. Should i use template way of implementation like below code :

template <class T> bool operator==(const T &v); 
3
  • 3
    To use a template is not the standard way. Commented May 13, 2014 at 16:31
  • Have you taken a look at cplusplus.com/doc/tutorial/templates ? Commented May 13, 2014 at 16:34
  • @juanchopanza - i have edited sample code.. Commented May 13, 2014 at 16:34

3 Answers 3

5

Don't use templates unless you need to. Does it make sense for Test to be comparable with any type? I doubt so.

Also there's no "Standard way" because the standard does not impose a strict way to implement operator== (I think it mentions something on the return type, but I doubt it actually enforces anything).

It's a good idea to make it return a bool and make it does what people expect it does (compare the two objects in a meaningful way).

And finally, you should mark your current one const:

bool operator==(const Test &v) const; 

Unless it actually modifies the invoking object, which is definitely something you don't want it to do.

Sign up to request clarification or add additional context in comments.

2 Comments

Incidentally, it is probably a good idea to implement operator!= too.
@MatteoItalia, yes. Possibly in terms of operator==. Like bool operator!=(T const& a, T const& b) { return !(a == b); }.
1

As others have mentioned, using a template without any reason to do so is neither standard nor recommended. However assuming you want operator== to be more like one of the default comparison operators you should either mark it const (otherwise you cannot compare two const Test) like so

class Test { public: Test(); virtual ~Test(); int x,y; bool operator==(const Test &v) const; }; // MyTest.cpp file bool Test::operator==(const Test &v) const { return (x== v.x) && (y==v.y); } 

, or make it a free function (in the same namespace for ADL) taking two const Test & (preferred) like so

class Test { public: Test(); virtual ~Test(); int x,y; }; bool operator==(const Test &a, const Test &b); // MyTest.cpp file bool operator==(const Test &a, const Test &b) { return (a.x==b.x) && (a.y==b.y); } 

If any access to private members is required the free function can always be marked friend. In simple cases such as the one in this example it can be advantageous to furthermore define it in the header and marking it inline.

Comments

1

Please do not use templates matching anything:

template <class T> bool operator==(const T &, const T &); 

That one will put you in all kinds of trouble !

2 Comments

ok..i have edited the code now .. which returns the bool value.
@user1291401 The comment is at the wrong spot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.