{"id":4351,"date":"2021-11-04T22:31:39","date_gmt":"2021-11-04T14:31:39","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=4351"},"modified":"2021-11-04T22:32:13","modified_gmt":"2021-11-04T14:32:13","slug":"c%e7%9a%84%e5%88%9d%e5%a7%8b%e5%8c%96%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=4351","title":{"rendered":"C++\u7684\u5bf9\u8c61\u521d\u59cb\u5316\u65b9\u5f0f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u65b9\u5f0f\u4e00\nint n=0;\nvoid* p=0;\nchar c='a';\nint n(0)\n\n\u65b9\u5f0f\u4e8c\nstruct S1\n{\n  explicit S1(int n, int m):x(n), y(m) {}\n  int x, y;\n}\nS1 s1(0,1);\n\n\n\u65b9\u5f0f\u4e09\nint c1[2] = {0, 2};\nchar c2 = \"message\";\nchar c3 = {'m', 's', 'g'};\nstruct S\n{\n  int a,b;\n}\nS s={0,1}\n\n\nc++03 \u4e0d\u80fd\u521d\u59cb\u5316\u52a8\u6001\u5206\u914d\u7684pod\u5bf9\u8c61,\u6bd4\u5982\uff1a\nclass C {\n  int x[100];\n  C();\n}\n\n\u4e5f\u6ca1\u6709\u597d\u529e\u6cd5\u521d\u59cb\u5316\uff0c\u53ea\u80fd\u8fd9\u6837\uff1a\nstd::vector &lt;std::string> vs;\nvs.push_back(\"alpha\");\nvs.push_back(\"beta\");\nvs.push_back(\"gamma\");\n\n\n\n\u800c\u5728C++11\u4e2d\u52a0\u5165\u4e86\u5927\u62ec\u53f7(\u82b1\u62ec\u53f7`{`)\u521d\u59cb\u5316\u8bed\u6cd5\uff1a\n\nint a{0};\nstd::string s{\"hello\"};\nstd::string s2{s}; \/\/ copy\nstd::vector&lt;std::string> vs {\"alpha\",\"bate\",\"gamma\"};\nstd::map&lt;string, string> stars { { \"s\", \"1\" }, {\"b\", \"2\" }};\ndouble* pd = new double[3] { 0.1, 0.2, 0.3};\nclass C {\n  int x[4];\n  C():x{0,1,2,3} {}\n}\n\n\nint n{}; \/\/ n\u7b49\u4e8e0\nint* p{}; \/\/ p\u7b49\u4e8enullptr\ndouble d{}; \/\/ d\u7b49\u4e8e0.0\nchar s[12] {}; \/\/ 12\u4e2a\u8fde\u7eed\u7684'\\0';\nchar* p=new char[5] {}; \/\/ 5\u4e2a'\\0';\n\n\/\/ \u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a7\nclass C\n{\n  int x=7;\n  int y[5] { 1, 2, 3, 4};\n  std::string s(\"abc\");\n  char* p { nullptr };\n}\n\u4ee5\u4e0a\u5f62\u5f0f\u7b49\u4ef7\u4e8e\nclass C\n{\n  int x;\n  int y[5];\n  std::string s;\n  char* p;\n  C(): s(\"abc\"), x(7), y{1,2,3,4}, p(nullptr) {}\n}\n\n\u6ce8\u610f\uff1a\n\u5982\u679c\u540c\u65f6\u5b58\u5728\u7c7b\u6210\u5458\u521d\u59cb\u5316\u5668\u548cMemInit\uff0c\u540e\u8005\u4f1a\u4f18\u5148\n\nstruct Class53 {\n    Class53(int v) :value(v) {\n        printf(\"init=%d\\n\", v);\n    }\n    int value;\n};\n\nstruct Class54 {\n    Class53 c53obj1{ 5 };\n    Class53 c53obj2{ 6 };\n    Class54(): c53obj1(7) {}\n};\n\nClass54 p54;;\nprintf(\"%d\\n\", p54.c53obj1.value);\nprintf(\"%d\\n\", p54.c53obj2.value);\n-------------\u6253\u5370\u51fa---------------\ninit=7\ninit=6\n7\n6<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4351"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4351"}],"version-history":[{"count":3,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4351\/revisions"}],"predecessor-version":[{"id":4354,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4351\/revisions\/4354"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}