0

Is there a way I can call an operator overload in C++ and call the parameter's function during the comparison?

For example:

class MyClass{
  private:
    int x;
    int y;
  public:
    MyClass(int x, int y);
    int getX();
    int getY();
    bool operator < (const MyClass &other) const {
        return (x < other.getX()); //this does not work!
        // this would work, though, if x was public:
        // return x < other.x;
    }
};

Basically, where I call other.getX(), how can I make it return its own x-value through a function to compare to the local one, instead of having to make x public? Is there a way of doing that?

Thanks for all your help!

6
  • It's a bit hard to get to the heart of your question. There is nothing wrong with your posted code, both other.getX() and other.x are valid (but getX() needs to be marked as const). Commented Sep 2, 2011 at 20:38
  • @Martin: no it doesn't work. See the answers. Commented Sep 2, 2011 at 20:38
  • @RTT: Yes, I noticed the const problem after commenting first. However, both calls were ok, only not const-correct. It's funny how some posts get 4 answers before one is able to write few lines of comments :) Commented Sep 2, 2011 at 20:42
  • @RTT: You really shouldn't mark my answer as accepted... Commented Sep 2, 2011 at 21:04
  • You can just do x < other.x a class is automatically a friend of itself. So you have full access to members of other instances of the same class. Commented Sep 2, 2011 at 21:08

4 Answers 4

9

You need to make the functions const, since you are using a reference to const MyClass:

int getX() const;

You can read about good use of const (const correctness) at:

Additionally I would recommend that you make the operator< a free function instead.

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

Comments

6

It doesn't work because getX() is not a const function. Change it to:

int getX() const;

and it will work. You could also remove the const in the operator argument, but that wouldn't normally be considered as good.

Comments

1

Access is per-class, not per-instance. You can access other.x with no problem.

Other than that, you could return a const reference, but it really wouldn't make any difference performance-wise and just looks weird.

bool operator < (const MyClass &other) const
{
    return x < other.x;
}

1 Comment

+1 This is the preferred method. I think its safe to assume you know how your own class works so it is safe to access members of other instances so there is no point in going through useless getter method.
-1

You can declare the operator as a friend of the class.

Edit: I seem to have tomatoes on my eyes, your operator is class member and already has private access. You could friend it however it was defined outside of class scope.

3 Comments

The operator is a member of the class, so already has all the access friend would give to a nonmember function.
Yes, I realized that after posting as well, hence my earlier edit.
A class is already a friend of itself. So it can access members of other instances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.