{"id":206239,"date":"2020-12-08T10:27:40","date_gmt":"2020-12-08T02:27:40","guid":{"rendered":"https:\/\/gulass.cn\/?p=206239"},"modified":"2020-12-02T10:21:36","modified_gmt":"2020-12-02T02:21:36","slug":"operator-overloading-c","status":"publish","type":"post","link":"https:\/\/gulass.cn\/operator-overloading-c.html","title":{"rendered":"\u6559\u4f60\u5feb\u901f\u7406\u89e3C++\u4e2d\u7684\u8fd0\u7b97\u7b26\u91cd\u8f7d"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728C ++\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u8fd0\u7b97\u7b26\u4e3a\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u5de5\u4f5c\u3002\u8fd9\u610f\u5473\u7740C ++\u80fd\u591f\u4e3a\u8fd0\u7b97\u7b26\u63d0\u4f9b\u6570\u636e\u7c7b\u578b\u7684\u7279\u6b8a\u542b\u4e49\uff0c\u8fd9\u79cd\u80fd\u529b\u79f0\u4e3a\u8fd0\u7b97\u7b26\u91cd\u8f7d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728C ++\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u8fd0\u7b97\u7b26\u4e3a\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u5de5\u4f5c\u3002\u8fd9\u610f\u5473\u7740C ++\u80fd\u591f\u4e3a\u8fd0\u7b97\u7b26\u63d0\u4f9b\u6570\u636e\u7c7b\u578b\u7684\u7279\u6b8a\u542b\u4e49\uff0c\u8fd9\u79cd\u80fd\u529b\u79f0\u4e3a\u8fd0\u7b97\u7b26\u91cd\u8f7d\u3002<\/p>\n

\"\"<\/p>\n

\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728String\u4e4b\u7c7b\u7684\u7c7b\u4e2d\u91cd\u8f7d\u8fd0\u7b97\u7b26'+'\uff0c\u4ee5\u4fbf\u4ec5\u4f7f\u7528+\u5c31\u53ef\u4ee5\u8fde\u63a5\u4e24\u4e2a\u5b57\u7b26\u4e32\u3002 \u7b97\u672f\u8fd0\u7b97\u7b26\u53ef\u80fd\u4f1a\u91cd\u8f7d\u7684\u5176\u4ed6\u793a\u4f8b\u7c7b\u662f\u590d\u6570\uff0c\u5c0f\u6570\uff0c\u5927\u6574\u6570\u7b49\u3002<\/p>\n

\u4e00\u4e2a\u7b80\u5355\u800c\u5b8c\u6574\u7684\u4f8b\u5b50<\/strong><\/div>\n
\r\n#include  \r\nusing namespace std;  \r\n   \r\nclass Complex {  \r\nprivate:  \r\n    int real, imag;  \r\npublic:  \r\n    Complex(int r = 0, int i =0)  {real = r;   imag = i;}  \r\n    Complex operator + (Complex const &obj) {  \r\n         Complex res;  \r\n         res.real = real + obj.real;  \r\n         res.imag = imag + obj.imag;  \r\n         return res;  \r\n    }  \r\n    void print() { cout << real << \" + i\" << imag << endl; }  \r\n};  \r\n   \r\nint main()  \r\n{  \r\n    Complex c1(10, 5), c2(2, 4);  \r\n    Complex c3 = c1 + c2;  \r\n    c3.print();  \r\n}  \r\n<\/iostream><\/pre>\n
\u64cd\u4f5c\u5458\u529f\u80fd\u548c\u666e\u901a\u529f\u80fd\u6709\u4ec0\u4e48\u533a\u522b?<\/strong><\/div>\n

\u64cd\u4f5c\u5458\u529f\u80fd\u4e0e\u666e\u901a\u529f\u80fd\u76f8\u540c\u3002\u552f\u4e00\u7684\u533a\u522b\u662f\uff0c\u8fd0\u7b97\u7b26\u7684\u540d\u79f0\u59cb\u7ec8\u662f\u8fd0\u7b97\u7b26\u5173\u952e\u5b57\uff0c\u540e\u8ddf\u8fd0\u7b97\u7b26\u7684\u7b26\u53f7\uff0c\u5e76\u4e14\u5728\u4f7f\u7528\u76f8\u5e94\u7684\u8fd0\u7b97\u7b26\u65f6\u4f1a\u8c03\u7528\u8fd0\u7b97\u7b26\u529f\u80fd\u3002<\/p>\n

\u4ee5\u4e0b\u662f\u5168\u5c40\u8fd0\u7b97\u7b26\u529f\u80fd\u7684\u793a\u4f8b\u3002<\/p>\n

\r\n#include  \r\nusing namespace std;  \r\n   \r\nclass Complex {  \r\nprivate:  \r\n    int real, imag;  \r\npublic:  \r\n    Complex(int r = 0, int i =0)  {real = r;   imag = i;}  \r\n    void print() { cout << real << \" + i\" << imag << endl; }  \r\n \r\nfriend Complex operator + (Complex const &, Complex const &);  \r\n};  \r\n   \r\n   \r\nComplex operator + (Complex const &c1, Complex const &c2)  \r\n{  \r\n     return Complex(c1.real + c2.real, c1.imag + c2.imag);  \r\n}  \r\n   \r\n   \r\nint main()  \r\n{  \r\n    Complex c1(10, 5), c2(2, 4);  \r\n    Complex c3 = c1 + c2;  \r\n    c3.print();  \r\n    return 0;  \r\n} \r\n<\/iostream><\/pre>\n
\u6211\u4eec\u53ef\u4ee5\u8ba9\u6240\u6709\u8fd0\u7b97\u7b26\u8d85\u8d1f\u8377\u5417?<\/strong><\/div>\n

\u9664\u4e86\u5c11\u6570\u64cd\u4f5c\u5458\u4e4b\u5916\uff0c\u51e0\u4e4e\u6240\u6709\u64cd\u4f5c\u5458\u90fd\u53ef\u4ee5\u91cd\u8f7d\u3002\u4ee5\u4e0b\u662f\u4e0d\u80fd\u91cd\u8f7d\u7684\u8fd0\u7b97\u7b26\u7684\u5217\u8868\u3002<\/p>\n

\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u3002(\u70b9)\uff0c::\uff0c?\uff1a\u548csizeof\u662f\u5426\u8fc7\u8f7d?<\/p>\n

\u8bf7\u53c2\u9605\u6b64\u4ee5\u83b7\u53d6Stroustrup\u81ea\u5df1\u7684\u7b54\u6848\u3002<\/p>\n

\u5173\u4e8e\u8fd0\u7b97\u7b26\u91cd\u8f7d\u7684\u8981\u70b9<\/strong><\/div>\n

1)\u4e3a\u4e86\u4f7f\u8fd0\u7b97\u7b26\u91cd\u8f7d\u8d77\u4f5c\u7528\uff0c\u81f3\u5c11\u4e00\u4e2a\u64cd\u4f5c\u6570\u5fc5\u987b\u662f\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u5bf9\u8c61\u3002<\/p>\n

2) \u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u4e3a\u6bcf\u4e2a\u7c7b\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002\u9ed8\u8ba4\u8d4b\u503c\u8fd0\u7b97\u7b26\u786e\u5b9e\u5c06\u53f3\u4fa7\u7684\u6240\u6709\u6210\u5458\u5206\u914d\u5230\u5de6\u4fa7\uff0c\u5e76\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c(\u6b64\u884c\u4e3a\u4e0e\u590d\u5236\u6784\u9020\u51fd\u6570\u76f8\u540c)\u3002\u8bf7\u53c2\u9605\u6b64\u4e86\u89e3\u66f4\u591a\u8be6\u60c5\u3002<\/p>\n

3) \u8f6c\u6362\u8fd0\u7b97\u7b26\uff1a\u6211\u4eec\u8fd8\u53ef\u4ee5\u7f16\u5199\u53ef\u7528\u4e8e\u5c06\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u8f6c\u6362\u8fd0\u7b97\u7b26\u3002<\/p>\n

\r\n#include   \r\nusing namespace std;  \r\nclass Fraction  \r\n{  \r\n    int num, den;  \r\npublic:  \r\n    Fraction(int n,  int d) { num = n; den = d; }  \r\n \r\n    operator float() const {  \r\n        return float(num) \/ float(den);  \r\n    }  \r\n};  \r\n   \r\nint main() {  \r\n    Fraction f(2, 5);  \r\n    float val = f;  \r\n    cout << val;  \r\n    return 0;  \r\n}  \r\n<\/iostream><\/pre>\n

\u91cd\u8f7d\u7684\u8f6c\u6362\u8fd0\u7b97\u7b26\u5fc5\u987b\u662f\u6210\u5458\u65b9\u6cd5\u3002\u5176\u4ed6\u8fd0\u7b97\u7b26\u53ef\u4ee5\u662f\u6210\u5458\u65b9\u6cd5\u6216\u5168\u5c40\u65b9\u6cd5\u3002<\/p>\n

4)\u4efb\u4f55\u53ef\u4ee5\u7528\u5355\u4e2a\u53c2\u6570\u8c03\u7528\u7684\u6784\u9020\u51fd\u6570\u90fd\u53ef\u4ee5\u7528\u4f5c\u8f6c\u6362\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u9690\u5f0f\u8f6c\u6362\u4e3a\u6b63\u5728\u6784\u9020\u7684\u7c7b\u3002<\/p>\n

\r\n#include   \r\nusing namespace std;  \r\n   \r\nclass Point  \r\n{  \r\nprivate:  \r\n    int x, y;  \r\npublic:  \r\n    Point(int i = 0, int j = 0) {  \r\n        x = i;   y = j;  \r\n    }  \r\n    void print() {  \r\n        cout << endl << \" x = \" << x << \", y = \" << y;  \r\n    }  \r\n};  \r\n   \r\nint main() {  \r\n    Point t(20, 20);  \r\n    t.print();  \r\n    t = 30;    \r\n    t.print();  \r\n    return 0;  \r\n} \r\n<\/iostream><\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5728C ++\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u8fd0\u7b97\u7b26\u4e3a\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\u5de5\u4f5c\u3002\u8fd9\u610f\u5473\u7740C ++\u80fd\u591f\u4e3a\u8fd0\u7b97\u7b26\u63d0\u4f9b\u6570\u636e\u7c7b\u578b\u7684\u7279\u6b8a\u542b\u4e49\uff0c\u8fd9\u79cd\u80fd\u529b\u79f0\u4e3a\u8fd0\u7b97\u7b26\u91cd\u8f7d\u3002<\/p>\n","protected":false},"author":1898,"featured_media":206241,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[623],"class_list":["post-206239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-623"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/206239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=206239"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/206239\/revisions"}],"predecessor-version":[{"id":206409,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/206239\/revisions\/206409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/206241"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=206239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=206239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=206239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}