重载解析,为什么优先匹配非const版本?
下面这个小程序,运行结果是: operator s1()
#include<iostream>using namespace std;struct s;struct s1{ const s* m_s;};struct s{ operator s1()const{ cout << "operator s1() const" << endl; return s1(); } operator s1(){ cout << "operator s1()" << endl; return s1(); }};void test(s1 obj){}int main(void){ test(s()); return 0;}
#include<iostream>using namespace std;struct s;struct s1{ const s* m_s;};struct s{ operator s1()const{ cout << "operator s1() const" << endl; return s1(); } operator s1(){ cout << "operator s1()" << endl; return s1(); }};void test(s1 obj){}int main(void){ const s sa;//这个const影响重载选择的版本 test(sa); return 0;}