int operator () () const { return _x; }
will be called when your const object.
Also returning the link is not the best design, it violates the rule of hiding data, the set/get functions are the best choice. And you will be confused when your line 4 is called or when line 5 is called.
I suggest rewriting:
class A{ public: explict A(int x) : x_(x) {} //int operator () () const { return x_; } // leave operator() for functor. operator int() const { return x_; } // use conversion function instead void setX(int x) { x_ = x; } private: int x_; //suggest use trailing `_` };
source share