-4

Recently I had an interview with C++ opening. They asked me to write comparison operator logic (== operator). Like:
if(value1 == value2)
where I had to write my own comparison operator logic rather than using the (==) operator. Can someone help me to find a solution for this?

  • 1
    What, you mean like overload the `operator==` function? Or make your own `compare` function? And what did you try? What is the problem you're having? Can you please elaborate? And no matter what, how do do it either way would have been though by [a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Nov 11 '17 at 06:09
  • They asked me to write logic to compare two values, and I wrote: if(value1 == value2) { }. Then they told me to create my own comparison logic, rather than using the (==) operator. – Sachin Jose Nov 11 '17 at 06:26
  • 2
    Typically you write comparison overload for types that don't already implement it; e.g. custom types. You don't state types for the values. There are many things interviewers might have been trying to test. We cannot read their minds, so it's impossible to answer your question. However, it's important to note that you clearly didn't understand the question, and failed to get clarification. This would reflect ***very poorly*** for you in the interview. NOTE: It won't matter how brilliant a programmer you are; if you're unable to clarify requirements, your _program is likely to be wrong_! – Disillusioned Nov 11 '17 at 08:04

1 Answers1

2

You want to write your own overloaded equality operator. It's pretty straightforward:

#include <iostream>

class A {
public:
    A(int x) : number { x } {}
    int number;
};

bool operator==(const A& left, const A& right)
{
    return left.number == right.number;
}

int main()
{
    A a(0);
    A b(1);
    std::cout << "a == b: " << (a == b) << "\n";
    return 0;
}
Rob Hansen
  • 317
  • 1
  • 4
  • Yes, operator overloading is one good strategy, but still, it doesn't seem like a good method. I read some comments regarding this in another discussion thread about using the comparison operator overloading: https://stackoverflow.com/questions/45589563/define-generic-comparison-operator I am confused about this. Is it good to use operator overloading in this case ? – Sachin Jose Nov 11 '17 at 06:19
  • @SachinJose The `==` operator is the natural way to compare for equality in C++, so it seems like a fine candidate for operator overloading if you need to compare two objects for equality. – Some programmer dude Nov 11 '17 at 06:22
  • @SachinJose Stop being so vague. What "_some comments_" are confusing you. How can you expect someone to help and answer you if you do not explain what you're struggling with! – Disillusioned Nov 11 '17 at 06:22
  • Why do you think overloading an equality operator is a bad strategy? There may be some cases where it's suboptimal, but as a general rule this is the way to provide reasonable semantics for user-defined types. Unless there's a specific reason why overloading an operator is a bad idea in your specific problem, default to thinking it's a good one. :) – Rob Hansen Nov 11 '17 at 06:22
  • @SachinJose PS: The question you link to is about a _generic_ overload operator. So what exactly is the relevance here? – Disillusioned Nov 11 '17 at 06:25
  • Sorry for the misunderstanding. I am not an expert in c++, I was asking my doubts regarding the task which was given to me in the interview. My doubt is, even if we use operator overloading in this case we still use comparison operator that is:. std::cout << "a == b: " << (a == b) << "\n"; So whats the point is asking to write our own comparison operator logic ? – Sachin Jose Nov 11 '17 at 06:44
  • @SachinJose: Nobody has any idea what you are talking about. Either your interviewer was confused, or you are failing to communicate what he was asking you. If you are unable to get clarification from the interviewer, I don't think there's anything anybody can do to help you. – Benjamin Lindley Nov 11 '17 at 06:52