Disambiguate pointer to overloaded const member functions and member operator

Viewed 107

I have a small class with the following member functions:

class Point3 {
    Point3 operator - (Point3 const & p) const {...} 
    Point3 operator - () const {...}

    const float &x() const {...}
    float& x() {...}
}

I need to pass these member functions as arguments to other functions, and of course using just &Point3::operator- and &Point3::x does not work.

Looking to similar questions online, I concluded that the syntax for making this kind of casts is (ReturnType (Class::*)(Args...)) &Class::Function. However, I tried for the binary operator - without success.

(Point3 (Point3::*)(Point3 const &)) &Point3::operator-

gives error: address of overloaded function 'operator-' does not match required type 'Point3 (const Point3 &)' note: candidate function has different qualifiers (expected unqualified but found 'const') [...]

For the x() member function I just don't have idea on where place the const related to the member function. How can I disambiguate these member functions?

1 Answers

The compiler was nice enough to tell you what exactly is wrong. You forgot const:

(Point3 (Point3::*)(Point3 const &) const) &Point3::operator-
//                                  ^~~~~

And for x (I assume you want the const overload):

(const float &(Point3::*)() const) &Point3::x
Related