{"rsdb":{"rid":"242340","subhead":"","postdate":"0","aid":"166855","fid":"49","uid":"1","topic":"1","content":"
\n

p0:\u5bfc\u8bfb<\/h2> \n

1\uff1a\u6784\u9020\u51fd\u6570\u58f0\u660e\u4e3aexplict\uff0c\u53ef\u4ee5\u7981\u6b62\u7f16\u8bd1\u5668\u6267\u884c\u975e\u9884\u671f\u7684\u7c7b\u578b\u8f6c\u6362(\u963b\u6b62\u9690\u5f0f\u8f6c\u6362\uff0c\u4f46\u53ef\u4ee5\u663e\u5f0f\u8f6c\u6362)\u3002<\/p> \n

2\uff1acopy\u6784\u9020\u51fd\u6570 \u7528\u6765\u201d\u4ee5\u540c\u7c7b\u578b\u5bf9\u8c61\u521d\u59cb\u5316\u81ea\u6211\u5bf9\u8c61\u201d<\/p> \n

copy\u8d4b\u503c\u64cd\u4f5c\u7b26 \u7528\u6765\u201d\u4ece\u53e6\u4e00\u4e2a\u540c\u7c7b\u578b\u5bf9\u8c61\u62f7\u8d1d\u5176\u503c\u5230\u81ea\u6211\u5bf9\u8c61\u201d\u3002<\/p> \n

\u4e24\u8005\u533a\u522b\u5728\u4e8e\u5982\u679c\u4e00\u4e2a\u65b0\u5bf9\u8c61\u662f\u88ab\u5b9a\u4e49\uff0c\u4e00\u5b9a\u4f1a\u8c03\u7528\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u4e0d\u53ef\u80fd\u662f\u8d4b\u503c\u64cd\u4f5c\uff0c<\/p> \n

\u5982\u679c\u6ca1\u6709\u65b0\u5bf9\u8c61\u88ab\u5b9a\u4e49\uff0c\u90a3\u5c31\u662f\u8d4b\u503c\u64cd\u4f5c\u88ab\u8c03\u7528\u3002<\/p> \n

\r\n    Widget w1; \r\n    Widget w2(w1); \/\/copy\u6784\u9020\u51fd\u6570\r\n    Widget w3 = w2;\/\/copy\u6784\u9020\u51fd\u6570\r\n    w1 = w2;   \/\/copy assignment\u64cd\u4f5c\u7b26<\/pre> \n 

p1:\u89c6C++<\/a>\u4e3a\u4e00\u4e2a\u8bed\u8a00\u8054\u90a6<\/h2> \n

\u56db\u4e2a\u6a21\u5757\uff1aC, object-oriented C++<\/a>\uff0c template C++\uff0c STL<\/p> \n

\u9ad8\u6548\uff1a\u5185\u7f6e\u7c7b\u578b\u4f20\u9012\u4f7f\u7528\u503c\u4f20\u9012\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u3002<\/p> \n

p2:\u5c3d\u91cf\u7528 const ,enum , inline \u66ff\u6362 #define<\/h2> \n

1:\u5b9a\u4e49\u5e38\u91cf\u6307\u9488 const char* const authorName = \u201cSping\u201d;<\/p> \n

const std::string authorName(\u201cSping\u201d);<\/p> \n

2:class \u4e13\u5c5e\u5e38\u91cf<\/p> \n

\r\n    \/\/.h\r\n    class GamePlayer\r\n    {\r\n        private:\r\n            static const int NumTurns = 5;  \/\/1:\u5e38\u91cf\u58f0\u660e\r\n            static const int NumTurn;       \/\/2:\u5e38\u91cf\u58f0\u660e\r\n            int scores[NumTurns];\r\n    };\r\n    \/\/.cpp\r\n    const int GamePlayer::NumTurns;   \/\/1:\u5e38\u91cf\u5b9a\u4e49  \/\/\u5bf9\u4e8eclass\u4e13\u5c5e\u5e38\u91cf,\u5982\u679c\u662f\u6574\u6570\u7c7b\u578b\u5e76\u4e14\u4e0d\u9700\u8981\u53d6\u5730\u5740\u7684\u8bdd\u53ef\u4ee5\u4e0d\u5b9a\u4e49\u3002\r\n    const int GamePlayer::NumTurn = 5;\/\/2:\u5e38\u91cf\u5b9a\u4e49<\/pre> \n 

3:\u4e00\u4e2a\u5c5e\u4e8e\u679a\u4e3e\u7c7b\u578b\u7684\u6570\u503c\u53ef\u6743\u5145ints\u4f7f\u7528, \u6bd4\u5982\u5e38\u89c1\u7684\u5bf9\u8bdd\u6846\u8d44\u6e90ID\u3002<\/p> \n

\r\n    class Game\r\n    {\r\n        private:\r\n            enum { NumTurns = 5 };\r\n            int scores[NumTurns];\r\n    };<\/pre> \n 

4:\u4f7f\u7528 template \u66ff\u6362\u5b8f\u5b9a\u4e49<\/p> \n

\r\n    \/\/\u5982\u4ee5a\u548cb\u7684\u8f83\u5927\u503c\u8c03\u7528f\u51fd\u6570\r\n     #define CALL_WITH_MAX(a,b) f( (a)>(b) ? (a):(b) ) \r\n    template \n  \n   \r\n    inline void callWithMax(const T& a, const T& b)\r\n    {\r\n        f(a > b ? a : b);\r\n    }\n  <\/typename><\/pre> \n 

p3:\u5c3d\u53ef\u80fd\u4f7f\u7528const<\/h2> \n

1\uff1a\u6307\u9488\u6307\u5411\u5e38\u91cf \u4e0e \u5e38\u91cf\u7684\u6307\u9488<\/p> \n

const\u5728\u5de6\u8fb9\uff0c\u8868\u793a\u88ab\u6307\u7269\u662f\u5e38\u91cf\uff1bconst\u5728<\/em>\u53f3\u8fb9\uff0c\u8868\u793a\u6307\u9488\u81ea\u8eab\u662f\u5e38\u91cf\uff1b<\/p> \n

\r\n    char greeting[] = "Hello";\r\n    const char* p = greeting; \/\/no-const *p, const data\r\n    char* const p = greeting; \/\/const *p, no-const data<\/pre> \n 

2\uff1a\u58f0\u660e\u4e3aconst\u53ef\u5e2e\u52a9\u7f16\u8bd1\u5668\u4fa6\u6d4b\u51fa\u9519\u8bef\u7528\u6cd5\uff0cconst\u53ef\u88ab\u65bd\u52a0\u4e8e\u4efb\u4f55\u4f5c\u7528\u57df\u5185\u7684\u5bf9\u8c61\u3001\u51fd\u6570\u53c2\u6570\u3001\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u3001\u6210\u5458\u51fd\u6570\u672c\u4f53\u3002<\/p> \n

3\uff1a\u6210\u5458\u51fd\u6570\u5982\u679c\u53ea\u662f\u5e38\u91cf\u6027\u4e0d\u540c\uff0c\u53ef\u4ee5\u88ab\u91cd\u8f7d<\/p> \n

4\uff1amutable(\u53ef\u53d8\u7684)\uff0c\u5173\u952e\u5b57mutable\u91ca\u653e\u6389non-static\u6210\u5458\u53d8\u91cf\u7684bitwise constness\u7ea6\u675f\u3002<\/p> \n

5\uff1a\u5f53const\u548c non-const\u6210\u5458\u51fd\u6570\u6709\u7740\u5b9e\u8d28\u7b49\u4ef7\u7684\u5b9e\u73b0\u65f6\uff0c\u4ee4non-const\u7248\u672c\u8c03\u7528const\u7248\u672c\u53ef\u907f\u514d\u4ee3\u7801\u91cd\u590d\u3002<\/p> \n

\r\n    class TextBlock\r\n    {\r\n        public:\r\n            const char& operator[](std::size_t position) const{\r\n                return text[position];\r\n            }\r\n            char& operator[](std::size_t positon){\r\n                return const_cast\n  \n   ( \r\n                    static_cast\n   \n    (*this)[position] ) \/\/1\uff1a\u4e3a*this\u52a0\u4e0aconst\u8c03\u7528const op[]; } \/\/2\uff1a\u5c06op[]\u8fd4\u56de\u503c\u7684const\u8f6c\u9664; private: std::string text; public: std::size_t length() const; private: char* pText; mutable std::size_t textLength; \/\/\u6700\u8fd1\u4e00\u6b21\u8ba1\u7b97\u7684\u6587\u672c\u957f\u5ea6 mutable bool lengthIsValid; \/\/\u76ee\u524d\u957f\u5ea6\u662f\u5426\u6709\u6548 }; std::size_t TextBlock\uff1a\uff1alength() const { if(!lengthIsValid){ textLength = std::strlen(pText); lengthIsValid = true; } }\n   <\/const>\n  <\/char&><\/pre> \n 

p4:\u786e\u5b9a\u5bf9\u8c61\u5457\u4f7f\u7528\u524d\u5df2\u5148\u88ab\u521d\u59cb\u5316<\/h2> \n

1\uff1aC++\u4e0d\u4fdd\u8bc1\u521d\u59cb\u5316\u5185\u7f6e\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p> \n

2\uff1a\u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf\u7684\u521d\u59cb\u5316\u53d1\u751f\u5728\u8fdb\u5165\u6784\u9020\u51fd\u6570\u4e4b\u524d\u3002<\/p> \n

3\uff1a\u6784\u9020\u51fd\u6570\u6700\u597d\u4f7f\u7528\u6210\u5458\u521d\u503c\u5217\uff0c\u800c\u4e0d\u8981\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\uff0c\u521d\u503c\u5217\u7684\u521d\u59cb\u5316\u6b21\u5e8f\u53ea\u4e0eclass\u4e2d\u7684\u58f0\u660e\u6b21\u5e8f\u4e00\u81f4\u3002<\/p> \n

4\uff1a\u4e3a\u514d\u9664\u201d\u8de8\u7f16\u8bd1\u5355\u5143\u4e4b\u521d\u59cb\u5316\u6b21\u5e8f\u201d\u95ee\u9898\uff0c\u5e94\u4ee5local static\u5bf9\u8c61\u66ff\u6362non-local static\u5bf9\u8c61\u3002<\/p> \n

\r\n    FileSystem& tfs()\r\n    {\r\n        static FileSystem fs;  \/\/\u51fd\u6570\u5185\u7684static\u5bf9\u8c61\u79f0\u4e3alocal static\u3002\r\n        return fs;\r\n    } \/\/\u4e5f\u5373\u662f\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f<\/pre> \n<\/div>","orderid":"0","title":"Effective C++\u4ecb\u7ecd","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"262","pages":"1","comments":"0","posttime":"2017-12-12 15:04:44","list":"1513062284","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Effective<\/A> \u4ecb\u7ecd<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.110.148.226","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Effective C++\u4ecb\u7ecd","lastview":"1714246635","digg_num":"7","digg_time":"1711236769","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}