看不懂的C++:运算符重载

 1 #include <stdio.h>
 2 
 3 struct S
 4 {
 5     S(double d){
 6     
 7     }
 8     bool operator==(const S &rhs) const{
 9         return true;
10     }
11 };
12 
13 struct K{
14     K(double d){
15     
16     }
17 };
18 
19 int main()
20 {
21     S s(1.0);
22     if (1.0==s){
23         printf("ok\n");
24     }
25 }

第22行,编译失败。double可以隐式转换为S,调用第8行的==重载函数。但是C++明确的拒绝了这一条道路。

posted @ 2018-04-04 13:38  thomas76  阅读(205)  评论(0编辑  收藏  举报