0

With user defined conversion in C++ using the operator keyword, for example

class X {
private:
   int _x;
public:
   operator int() const { return _x; } 
};

how does the const qualifier change anything vs. not have a const qualifier? It seems that regardless of having const or no const, I would still need to assign to an lvalue like this:

X x;
const int &foo = x; 
solstice333
  • 3,399
  • 1
  • 31
  • 28
  • 2
    Non-const operator won't work when an instance of `X` itself is const: `const X x; int n = x;` [Demo](http://rextester.com/VGB55267) – Igor Tandetnik Jan 27 '16 at 00:32
  • 1
    There's no need for the reference, it only introduces a needless temporary (with life extension). The `const`-ness of a member function determines whether it can be called on a `const` instance of the class. – Cheers and hth. - Alf Jan 27 '16 at 00:33
  • Oh whoops, yes, this is a duplicate. Sorry about that. I kept searching around a limited context of a user defined conversion using the operator keyword when also used with const. – solstice333 Jan 27 '16 at 00:53

0 Answers0