James McNellis & rsquo; the answer did cover it all, but he is not afraid (hopefully) with more detailed explanations.
So.
When you call & hellip;
o.operator int()
& hellip; then the choice of overload depends entirely on the constant o .
Nothing more.
To find out why, consider this class:
struct Bar { void f() {} void f() const {} };
Technically, these member functions do not have to be member functions. They could also be selected as free functions. But then they need the Bar argument:
struct Bar {}; void f( Bar& ) {} void f( Bar const& ) {}
And hopefully now it’s easier to see that when you do
Bar o; f( o );
then you can select the first function. And so it is. Because if the second function is selected, you can never get the first. Because if you make a const object, then it breaks the correctness of const to select the first one. Therefore, when the object is const , only the second can be selected, therefore, when it is not const , the first is selected.
In short, the only practical alternative to this rule would always be to choose the second, which would make the first useless, right?
Cheers and hth.,
source share