{"id":379146,"date":"2021-02-21T17:33:52","date_gmt":"2021-02-21T09:33:52","guid":{"rendered":"http:\/\/4563.org\/?p=379146"},"modified":"2021-02-21T17:33:52","modified_gmt":"2021-02-21T09:33:52","slug":"%e5%85%b3%e4%ba%8e-c-default-constructor-%e7%9a%84%e7%96%91%e9%97%ae","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=379146","title":{"rendered":"\u5173\u4e8e C++ default constructor \u7684\u7591\u95ee"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e C++ default constructor \u7684\u7591\u95ee               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u770b Essential C++\u8fd9\u672c\u4e66<\/p>\n<p>\u6784\u9020\u5668\u8fd9\u5757\u5b9e\u5728\u5f88\u96be\u7406\u89e3\uff0c\u4e5f\u53ef\u80fd\u662f\u5176\u4ed6\u8bed\u8a00\u5148\u5165\u4e3a\u4e3b\u5bfc\u81f4\u7684&#8230;<\/p>\n<p>\u4e66\u4e0a\u8bf4\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6709\u4e24\u79cd\u5f62\u5f0f\uff0c\u4e00\u79cd\u662f\u65e0\u53c2\uff0c\u4e00\u79cd\u662f\u7ed9\u6bcf\u4e00\u4e2a\u53c2\u6570\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<p>\u5982\u679c\u6211\u8fd9\u4e24\u79cd\u5f62\u5f0f\u90fd\u5b9a\u4e49\u4e86\uff0c\u8fd9\u65f6\u5019<code>Triangular t1;<\/code>\u5c31\u4f1a\u51fa\u73b0 ambiguous \u7684\u7f16\u8bd1\u9519\u8bef\uff0c\u662f\u56e0\u4e3a <code>Triangular(int len = 1, int bp = 1)<\/code>\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4e5f\u88ab\u8ba4\u4e3a\u662f\u65e0\u53c2\u6784\u9020\uff1f<\/p>\n<p>\u4f46\u662f\u6211\u5982\u679c\u518d\u5b9a\u4e49\u4e00\u4e2a<code>Triangular(int len, int bp);<\/code>\u4e5f\u4e0d\u884c\uff0c\u8fd9\u65f6\u5019<code>Triangular(int len = 1, int bp = 1)<\/code>\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u662f\u88ab\u8ba4\u4e3a\u662f\u4e24\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\uff1f<\/p>\n<p>\u7136\u540e\u5982\u679c\u6211\u7684\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u5728 class \u5916\uff0c\u53d8\u6210<code>Triangular::Triangular(int len, int bp){}<\/code>\u548c<code>Triangular::Triangular(){}<\/code>\uff0c\u8fd9\u4e5f\u5b8c\u5168\u770b\u4e0d\u51fa\u8fd9\u662f\u4e24\u4e2a\u51b2\u7a81\u7684 default constructor \u4e86\u3002\u3002<\/p>\n<p>\u8fd9\u51e0\u4e2a\u5173\u7cfb\u641e\u5f97\u6709\u70b9\u4e71\uff0c\u6709\u5927\u4f6c\u8bf4\u8bf4 C++\u8fd9\u5757\u7684\u8bbe\u8ba1\u54f2\u5b66\u5417\u3002\u3002\u3002\u603b\u611f\u89c9\u6709\u70b9\u96be\u8bb0<\/p>\n<pre><code>class Triangular { public:     Triangular(); \/\/    Triangular(int len);     Triangular(int len = 1, int bp = 1); \/\/    Triangular(int len, int bp);  private:     int _length;     int _beg_pos;     int _next; };  Triangular::Triangular(int len, int bp) {     _length = len &gt; 0 ? len : 1;     _beg_pos = bp &gt; 0 ? bp : 1;     _next = _beg_pos - 1; }  Triangular::Triangular() {     _length = 1;     _beg_pos = 1;     _next = 0; }   int main() { \/\/    Triangular t1;                   \/\/ \u65e0\u53c2\u6784\u9020     Triangular t2 = 5;                 \/\/ \u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570     Triangular t3(1);            \/\/ \u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570     Triangular t4(1, 2);  \/\/ \u4e24\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570 \/\/    Triangular t5();                 \/\/ \u4e3a\u4e86\u517c\u5bb9 C\uff0c\u8fd9\u88ab\u8ba4\u4e3a\u662f\u51fd\u6570\u8c03\u7528\u800c\u4e0d\u662f\u65e0\u53c2\u6784\u9020     return 0; } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5462617\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hello2060 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5df2\u7ecf\u5fd8\u4e86 c++\u4e86\uff0c\u968f\u4fbf\u731c\u731c<\/p>\n<p>\u7b2c\u4e00\u79cd\u60c5\u51b5\uff0c\u4e00\u4e2a\u65e0\u53c2\u6570\uff0c\u4e00\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u51b2\u7a81\u662f\u6b63\u5e38\u7684\u554a\u3002<\/p>\n<p>\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u4e00\u4e2a\u65e0\u53c2\u6570\uff0c\u4e00\u4e2a\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e8c\u4e2a\u4e0d\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4e5f\u5f88\u6b63\u5e38\u554a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462618\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hello2060 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u624b\u673a\u4e0a\u770b\u8d34\u7684\uff0c\u4f3c\u4e4e\u4f60\u8bf4\u7684\u662f\u7b2c\u4e8c\u79cd\u60c5\u51b5\u8fd8\u662f\u6709\u9ed8\u8ba4\u53c2\u6570\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462619\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : anonymous256 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u201c\u662f\u56e0\u4e3a Triangular(int len = 1, int bp = 1)\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4e5f\u88ab\u8ba4\u4e3a\u662f\u65e0\u53c2\u6784\u9020\uff1f\u201d \u662f\u554a<br \/>\u4f60\u6ca1\u63d0\u4f9b\u53c2\u6570\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u9009\u62e9\u54ea\u4e2a\u6784\u9020\u51fd\u6570\u4e86\u3002 \u4f60\u9700\u8981\u79fb\u9664\u4e00\u4e2a\u6784\u9020\u51fd\u6570                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462620\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hello2060 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u7684 code \u91cc\u8fd9\u79cd\u5199\u6cd5\u548c\u7b2c\u4e00\u79cd\u662f\u4e00\u6837\u7684\u5427\uff0c\u9ed8\u8ba4\u53c2\u6570\u53ea\u8981\u5728\u58f0\u660e\u91cc\u6307\u5b9a\uff0c\u5b9a\u4e49\u91cc\u4e0d\u7528\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462621\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ipwx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8230; \u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u79cd\u6709\u6b67\u4e49\u7684\u8bed\u6cd5\u3002\u3002\u3002<\/p>\n<p>\u4f60\u96be\u9053\u4f1a\u5b9a\u4e49\u4e24\u4e2a\u51fd\u6570\uff0c\u4e00\u4e2a int f(); \u4e00\u4e2a int f(int a=1, int b=2); \u561b\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462622\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wutiantong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u4f60\u5b9a\u4e49\u51fd\u6570\u65f6\u6307\u5b9a\u4e86\u4e00\u4e9b\u53c2\u6570<br \/>2. \u4f60\u4e3a\u51fd\u6570\u53c2\u6570\u914d\u7f6e\u4e86\u4e00\u4e9b\u9ed8\u8ba4\u503c<br \/>3. \u4f60\u5199\u4e00\u53e5\u8c03\u7528\u51fd\u6570\u7684\u8868\u8fbe\u5f0f\u65f6\uff0c\u7f16\u8bd1\u5668\u5c1d\u8bd5\u901a\u8fc7\u5404\u79cd\u4e0a\u4e0b\u6587\u4fe1\u606f\u6765\u627e\u5230\u6b63\u786e\u7684\u88ab\u8c03\u7528\u51fd\u6570\uff08\u9759\u6001\u91cd\u8f7d\uff09<\/p>\n<p>\u4ee5\u4e0a\u662f\u4e09\u4ef6\u72ec\u7acb\u7684\u4e8b\u60c5\uff0c\u4e0d\u8981\u6df7\u6dc6\u5728\u4e00\u8d77\u7406\u89e3\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462623\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yulon <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u6ca1\u6709\u51b2\u7a81\u7684\u8bdd\uff0c\u4f60\u89c9\u5f97\u4f60\u8981\u600e\u4e48\u8c03\u7528\uff0c\u624d\u80fd\u5229\u7528\u5230 len = 1 \u8fd9\u4e2a\u9ed8\u8ba4\u503c\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462624\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GeruzoniAnsasu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9996\u5148\u50cf #6 \u8bf4\u7684\u5148\u628a\u9ed8\u8ba4\u6784\u9020 \/ \u6784\u9020\u51fd\u6570\u5e26\u6709\u9ed8\u8ba4\u53c2\u6570 \/ \u7f16\u8bd1\u5668\u9009\u62e9\u91cd\u8f7d\u8fd9\u4e09\u8005\u533a\u5206\u5f00\u6765<\/p>\n<p>Class() \u65e0\u53c2\u9ed8\u8ba4\u6784\u9020<br \/>Class(type param=1) \u5e26\u6709\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\uff0c\u4e14\u8fd9\u4e2a\u51fd\u6570\u6709\u9ed8\u8ba4\u503c<\/p>\n<p>\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u8fd9\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u5e94\u8be5\u4e0d\u96be\u7406\u89e3<\/p>\n<p>\u7136\u540e\u5982\u679c\u4e00\u4e2a\u7c7b\u540c\u65f6\u5b58\u5728\u8fd9\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5f53\u4f60\u5199\u4e0b Class obj \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bfb\u627e\u53ef\u4ee5\u6ee1\u8db3\u5bf9\u8c61\u5b9a\u4e49\u8bed\u4e49\u7684\u6784\u9020\u51fd\u6570\uff0c\u4e8e\u662f\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u80fd\u88ab\u627e\u5230\uff0c\u4e8e\u662f\u4f1a\u4ea7\u751f ambiguous \u3002\u5f53\u4f60\u5199 Class obj(1) \u7684\u65f6\u5019\uff0c\u65e0\u53c2\u6784\u9020\u4e0d\u80fd\u6ee1\u8db3\u8bed\u4e49\uff0c\u53ea\u6709\u7b2c\u4e8c\u4e2a\u6784\u9020\u53ef\u4ee5\u6ee1\u8db3\uff0c\u56e0\u6b64\u65e0\u6b67\u4e49                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5462625\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dangyuluo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u7f16\u8bd1\u5668\u62a5\u9519\uff0c\u627e candidate \u5173\u952e\u8bcd                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e C++ default co&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/379146"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=379146"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/379146\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=379146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=379146"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=379146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}