{"id":115,"date":"2003-05-12T18:18:00","date_gmt":"2003-05-12T10:18:00","guid":{"rendered":"http:\/\/www.freemindworld.com\/blog\/2003\/05\/0513_%e5%9c%a8c%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%b1%9e%e6%80%a7-property.shtml"},"modified":"2012-01-30T17:02:04","modified_gmt":"2012-01-30T09:02:04","slug":"030512_cpp_property","status":"publish","type":"post","link":"https:\/\/www.freemindworld.com\/blog\/2003\/030512_cpp_property.shtml","title":{"rendered":"[\u7ffb\u8bd1] \u5728C++\u4e2d\u5b9e\u73b0\u201c\u5c5e\u6027 (Property)\u201d"},"content":{"rendered":"<p align=\"center\"><span style=\"font-size: large;\"><span style=\"font-family: \u96b6\u4e66;\">\u5728C++\u4e2d\u5b9e\u73b0\u201c\u5c5e\u6027 (Property)\u201d<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\"><strong>\u6458\u8981\uff1a<\/strong><\/span><\/p>\n<p><span style=\"font-size: small;\">\u672c\u6587\u4ecb\u7ecd\u4e86\u5728C++\u4e2d\u5b9e\u73b0\u201c\u5c5e\u6027 (Property)\u201d\u7684\u65b9\u6cd5\uff0c\u201c\u5c5e\u6027\u201d\u662f\u6211\u4eec\u5728C#\uff08\u6216\u5176\u5b83\u4e00\u4e9b\u8bed\u8a00\uff09\u4e2d\u5e38\u5e38\u80fd\u7528\u5230\u7684\u4e00\u79cd\u7279\u6027\u3002\u8fd9\u91cc\u4ecb\u7ecd\u7684\u5b9e\u73b0\u65b9\u6cd5\u4f7f\u7528\u7684\u662f\u6807\u51c6\u7684C++\uff0c\u6ca1\u6709\u7528\u4efb\u4f55\u5176\u5b83\u7684\u8bed\u8a00\u6269\u5c55\u3002\u800c\u5927\u90e8\u5206\u7684\u5e93\u6216\u662f\u7f16\u8bd1\u5668\u4e3a\u4e86\u5b9e\u73b0\u201c\u5c5e\u6027\u201d\uff0c\u5f80\u5f80\u5bf9C++\u4f5c\u4e00\u4e9b\u6269\u5c55\uff0c\u5c31\u50cf\u6211\u4eec\u5728\u6258\u7ba1\u7684C++\u6216\u662fC++ Builder\u4e2d\u770b\u5230\u7684\u90a3\u6837\uff0c\u4e5f\u6709\u7684\u662f\u4f7f\u7528\u666e\u901a\u7684set\u548cget\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u90fd\u4e0d\u80fd\u7b97\u662f\u771f\u6b63\u7684\u201c\u5c5e\u6027\u201d\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\"><strong>\u6b63\u6587\uff1a<\/strong><\/span><\/p>\n<p><span style=\"font-size: small;\">\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4ec0\u4e48\u662f\u201c\u5c5e\u6027\u201d\u3002\u201c\u5c5e\u6027\u201d\u5728\u5916\u89c2\u4e0a\u770b\u8d77\u6765\u5c31\u50cf\u7c7b\u4e2d\u7684\u4e00\u4e2a\u666e\u901a\u6210\u5458\u53d8\u91cf\uff08\u6216\u8005\u79f0\u4e3a\u662f\u201c\u5b57\u6bb5\u201d\uff09\uff0c\u4f46\u5b83\u5185\u90e8\u662f\u901a\u8fc7\u4e00\u7ec4set\/get\u65b9\u6cd5\uff08\u6216\u79f0\u4f5cread\/write\u65b9\u6cd5\uff09\u6765\u8bbf\u95ee\u7c7b\u4e2d\u5b9e\u9645\u7684\u6210\u5458\u53d8\u91cf\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u6211\u6709\u4e00\u4e2a\u7c7b<span style=\"font-family: Courier New;\">A<\/span>\u548c\u5b83\u7684\u4e00\u4e2a\u201c\u5c5e\u6027\u201d<span style=\"font-family: Courier New;\">Count<\/span>\uff0c\u6211\u5c31\u53ef\u4ee5\u5199\u51fa\u5982\u4e0b\u7684\u4ee3\u7801\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">A foo;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">cout &lt;&lt; foo.Count;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: Courier New;\">Count<\/span>\u5b9e\u9645\u4e0a\u662f\u8c03\u7528\u4e86\u4e00\u4e2a<\/span>get\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e86\u6211\u4eec\u6240\u5e0c\u671b\u5f97\u5230\u7684\u6210\u5458\u53d8\u91cf\u503c\u3002\u4f7f\u7528\u201c\u5c5e\u6027\u201d\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u6210\u5458\u53d8\u91cf\u503c\u7684\u6700\u5927\u597d\u5904\u662f\u4f60\u53ef\u63a7\u5236\u8fd9\u4e2a\u201c\u5c5e\u6027\u201d\u662f\u53ea\u8bfb\u7684\uff08\u60a8\u53ea\u80fd\u8bfb\u51fa\u5b83\u7684\u503c\u800c\u4e0d\u80fd\u6539\u53d8\u5b83\u7684\u503c\uff09\u3001\u53ea\u5199\u7684\u3001\u6216\u662f\u53ef\u8bfb\u53ef\u5199\u7684\u3002\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u5b9e\u73b0\u5b83\u5427\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6211\u4eec\u5e0c\u671b\u80fd\u5b9e\u73b0\u4e0b\u9762\u7684\u7528\u6cd5\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">int i = foo.Count; \/\/&#8211; \u5b9e\u9645\u4e0a\u8c03\u7528get\u51fd\u6570\u6765\u83b7\u53d6\u5b9e\u9645\u7684\u6210\u5458\u53d8\u91cf\u7684\u503c &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">foo.Count = i;\u3000\u3000 \/\/&#8211; \u5b9e\u9645\u4e0a\u5c06\u8c03\u7528set\u51fd\u6570\u6765\u8bbe\u7f6e\u5b9e\u9645\u7684\u6210\u5458\u53d8\u91cf\u7684\u503c &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u56e0\u6b64\uff0c\u5f88\u660e\u663e\u7684\uff0c\u6211\u4eec\u9700\u8981\u91cd\u8f7d\u201c=\u201d\u8fd0\u7b97\u7b26\u4ee5\u4fbf\u53ef\u4ee5\u8bbe\u7f6e\u201c\u5c5e\u6027\u201d\u7684\u503c\uff0c\u8fd8\u8981\u6b63\u786e\u5904\u7406\u201c\u5c5e\u6027\u201d\u7684\u8fd4\u56de\u7c7b\u578b\uff08\u7a0d\u540e\u5c31\u53ef\u4ee5\u770b\u5230\u4e00\u70b9\uff09\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u7c7b\uff0c\u540d\u53eb<span style=\"font-family: Courier New;\">property<\/span>\uff0c\u5b83\u5c06\u8868\u73b0\u5f97\u50cf\u4e00\u4e2a\u201c\u5c5e\u6027\u201d\uff0c\u5b83\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">template&lt;typename Container, typename ValueType, int nPropType&gt;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">class property {}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u4e2a\u7c7b\u6a21\u7248\u5c06\u8868\u73b0\u4e3a\u6211\u4eec\u9700\u8981\u7684\u201c\u5c5e\u6027\u201d\u3002<span style=\"font-family: Courier New;\">Container<\/span>\u662f\u4e00\u4e2a\u7c7b\u7684\u7c7b\u578b\uff08\u540e\u9762\u6211\u4eec\u79f0\u4e4b\u4e3a\u201c\u5bb9\u91cf\u7c7b\u201d\uff09\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u662f\u5305\u542b\u8981\u5b9e\u73b0\u4e3a\u201c\u5c5e\u6027\u201d\u7684\u5b9e\u9645\u6210\u5458\u53d8\u91cf\u3001\u8bbf\u95ee\u8fd9\u4e2a\u53d8\u91cf\u7684set\/get\u65b9\u6cd5\u548c\u8868\u73b0\u51fa\u6765\u7684\u201c\u5c5e\u6027\u201d\u7684\u7c7b\u3002<span style=\"font-family: Courier New;\">ValueType<\/span>\u662f\u5bb9\u91cf\u7c7b\u5185\u90e8\u7684\u5b9e\u9645\u6210\u5458\u53d8\u91cf\u7684\u7c7b\u578b\uff08\u4e5f\u5c06\u6210\u4e3a\u201c\u5c5e\u6027\u201d\u7684\u7c7b\u578b\uff09\uff0c<span style=\"font-family: Courier New;\">nPropType<\/span>\u8868\u793a\u201c\u5c5e\u6027\u201d\u7684\u7c7b\u522b\uff1a\u201c\u53ea\u8bfb\u201d\u3001\u201c\u53ea\u5199\u201d\u6216\u662f\u201c\u8bfb\u5199\u201d\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6211\u4eec\u8fd8\u9700\u8981\u8bbe\u7f6e\u4e00\u7ec4\u6307\u9488\uff0c\u6307\u5411\u5bb9\u5668\u7c7b\u7279\u5b9a\u6210\u5458\u53d8\u91cf\u7684set\u548cget\u65b9\u6cd5\uff0c\u540c\u65f6\u8fd8\u8981\u91cd\u8f7d\u201c=\u201d\u8fd0\u7b97\u7b26\uff0c\u4f7f\u5f97\u201c\u5c5e\u6027\u201d\u53ef\u4ee5\u8868\u73b0\u5f97\u50cf\u4e00\u4e2a\u53d8\u91cf\u3002\u4e0b\u9762\u6211\u4eec\u770b\u770b<span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: Courier New;\">property<\/span>\u7c7b\u7684\u5b8c\u6574\u7a0b\u5e8f\u3002<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">#define READ_ONLY 1<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">#define WRITE_ONLY 2<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">#define READ_WRITE 3<\/span><\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">template &lt;typename Container, typename ValueType, int nPropType&gt;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">class property<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">public:<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">property()<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 m_cObject = NULL;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 Set = NULL;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 Get = NULL;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; This to set a pointer to the class that contain the<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/\u00a0\u00a0 property &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">void setContainer(Container* cObject)<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 m_cObject = cObject;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; Set the set member function that will change the value &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">void setter(void (Container::*pSet)(ValueType value))<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE))<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Set = pSet;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 else<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Set = NULL;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; Set the get member function that will retrieve the value &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">void getter(ValueType (Container::*pGet)())<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 if((nPropType == READ_ONLY) || (nPropType == READ_WRITE))<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Get = pGet;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 else<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Get = NULL;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; Overload the &#8216;=&#8217; sign to set the value using the set<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/\u00a0\u00a0 member &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">ValueType operator =(const ValueType&amp; value)<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(m_cObject != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(Set != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 (m_cObject-&gt;*Set)(value);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 return value;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; To make possible to cast the property class to the<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/\u00a0\u00a0 internal type &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">operator ValueType()<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(m_cObject != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(Get != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 return (m_cObject-&gt;*Get)();<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">private:<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 Container* m_cObject;\u00a0 \/\/&#8211; Pointer to the module that<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u00a0\u00a0 contains the property &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 void (Container::*Set)(ValueType value);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/&#8211; Pointer to set member function &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 ValueType (Container::*Get)();<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/&#8211; Pointer to get member function &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">};<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8ba9\u6211\u4eec\u6765\u4e00\u6bb5\u6bb5\u7684\u5206\u6790\u7a0b\u5e8f\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u628a<span style=\"font-family: Courier New;\">Container<\/span>\u6307\u9488\u6307\u5411\u4e00\u4e2a\u6709\u6548\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u662f\u6211\u4eec\u8981\u6dfb\u52a0\u201c\u5c5e\u6027\u201d\u7684\u7c7b\uff08\u4e5f\u5c31\u662f\u5bb9\u5668\u7c7b\uff09\u7684\u5bf9\u8c61\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">void setContainer(Container * cObject)<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{ <\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 m_cObject = cObject;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u8bbe\u7f6e\u6307\u9488\u6307\u5411\u5bb9\u5668\u7c7b\u7684set\/get\u6210\u5458\u51fd\u6570\u3002\u8fd9\u91cc\u4ec5\u6709\u7684\u4e00\u70b9\u9650\u5236\u662fset\u51fd\u6570\u5fc5\u987b\u662f\u5e26\u4e00\u4e2a\u53c2\u6570\u4e14\u8fd4\u56devoid\u7684\u51fd\u6570\uff0c\u800cget\u51fd\u6570\u5fc5\u987b\u4e0d\u5e26\u53c2\u6570\u4e14\u8fd4\u56de<span style=\"font-family: Courier New;\">ValueType<\/span>\u578b\u7684\u503c\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; Set the set member function that will change the value &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">void setter(void (Container::*pSet)(ValueType value))<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE))<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Set = pSet;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 else<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Set = NULL;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; Set the get member function that will retrieve the value &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">void getter(ValueType (Container::*pGet)())<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 if((nPropType == READ_ONLY) || (nPropType == READ_WRITE))<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Get = pGet;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 else<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Get = NULL;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u9996\u5148\u662f\u5bf9\u201c=\u201d\u8fd0\u7b97\u7b26\u8fdb\u884c\u4e86\u91cd\u8f7d\uff0c\u5b83\u8c03\u7528\u4e86\u5bb9\u5668\u7c7b\u7684set\u6210\u5458\u51fd\u6570\u4ee5\u5b9e\u73b0\u8d4b\u503c\u64cd\u4f5c\u3002\u7136\u540e\u662f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8f6c\u6362\u51fd\u6570\uff0c\u5b83\u8fd4\u56deget\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u8fd9\u4f7f\u6574\u4e2a<span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: Courier New;\">property<\/span>\u7c7b\u8868\u73b0\u5f97\u50cf\u4e00\u4e2a<\/span><span style=\"font-family: Courier New;\">ValueType<\/span>\u578b\u7684\u6210\u5458\u53d8\u91cf\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; Overload the &#8216;=&#8217; sign to set the value using the set member &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">ValueType operator =(const ValueType&amp; value)<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(m_cObject != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(Set != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 (m_cObject-&gt;*Set)(value);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 return value;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/&#8211; To make possible to cast the property class to the<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\/\/\u00a0\u00a0 internal type &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">operator ValueType()<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(m_cObject != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 assert(Get != NULL);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 return (m_cObject-&gt;*Get)();<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">}<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u8ba9\u6211\u4eec\u770b\u770b\u6211\u4eec\u662f\u5982\u4f55\u6765\u4f7f\u7528\u8fd9\u4e2a<span style=\"font-family: Courier New;\">property<\/span>\u7c7b\u7684\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5c31\u50cf\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\u6240\u5c55\u793a\u7684\u4e00\u6837\uff1a<span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: Courier New;\">PropTest<\/span>\u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u540d\u4e3a<\/span><span style=\"font-family: Courier New;\">Count<\/span>\u7684\u201c\u5c5e\u6027\u201d\u3002\u8fd9\u4e2a\u201c\u5c5e\u6027\u201d\u7684\u503c\u5b9e\u9645\u4e0a\u662f\u901a\u8fc7get\u51fd\u6570\u4ece\u4e00\u4e2a\u540d\u4e3a<span style=\"font-family: Courier New;\">m_nCount<\/span>\u7684\u79c1\u6709\u53d8\u91cf\u83b7\u5f97\u5e76\u901a\u8fc7set\u51fd\u6570\u628a\u8fd9\u4e2a\u201c\u5c5e\u6027\u201d\u503c\u7684\u53d8\u52a8\u5199\u56de\u5230<span style=\"font-family: Courier New;\">m_nCount<\/span>\u4e2d\u53bb\u7684\u3002get\/set\u51fd\u6570\u53ef\u4ee5\u4efb\u610f\u547d\u540d\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u901a\u8fc7\u5b83\u4eec\u7684\u51fd\u6570\u5730\u5740\u4f20\u9012\u7ed9<span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: Courier New;\">property<\/span>\u7c7b\u7684\uff0c\u5c31\u50cf\u60a8\u5728<\/span><span style=\"font-family: Courier New;\">PropTest<\/span>\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u770b\u5230\u7684\u90a3\u6837\u3002<span style=\"font-family: Courier New;\">PropTest<\/span>\u7c7b\u4e2d\u7684&#8221;<span style=\"font-family: Courier New;\">property&lt;PropTest,int,READ_WRITE&gt; Count;<\/span> &#8220;\u4e00\u884c\u5c31\u4f7f\u6211\u4eec\u7684<span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: Courier New;\">PropTest<\/span>\u7c7b\u5c31\u62e5\u6709\u4e86\u4e00\u4e2a\u540d\u53eb<\/span><span style=\"font-family: Courier New;\">Count<\/span>\u53ef\u4ee5\u88ab\u8bfb\u5199\u7684\u6574\u578b\u201c\u5c5e\u6027\u201d\u4e86\u3002\u60a8\u53ef\u4ee5\u628a<span style=\"font-family: Courier New;\">Count<\/span>\u5f53\u6210\u662f\u4e00\u4e2a\u666e\u901a\u7684\u6210\u5458\u53d8\u91cf\u4e00\u6837\u6765\u4f7f\u7528\uff0c\u800c\u5b9e\u9645\u4e0a\uff0c\u5bf9<span style=\"font-family: Courier New;\">Count<\/span>\u7684\u8bfb\u5199\u90fd\u662f\u901a\u8fc7set\/get\u51fd\u6570\u95f4\u63a5\u7684\u5b9e\u73b0\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">PropTest<\/span>\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u6240\u505a\u7684\u521d\u59cb\u5316\u5de5\u4f5c\u662f\u5fc5\u987b\u7684\uff0c\u53ea\u6709\u8fd9\u6837\u624d\u80fd\u4fdd\u8bc1\u5b9a\u4e49\u7684\u201c\u5c5e\u6027\u201d\u53ef\u4ee5\u6b63\u5e38\u7684\u5de5\u4f5c\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">class PropTest<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">{<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">public:<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 PropTest()<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 {<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Count.setContainer(this);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Count.setter(&amp;PropTest::setCount);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 Count.getter(&amp;PropTest::getCount);<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 }<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 int getCount()<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 {<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 return m_nCount;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 }<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 void setCount(int nCount)<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 {<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0\u00a0\u00a0 m_nCount = nCount;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 }<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 property&lt;PropTest,int,READ_WRITE&gt; Count;<\/span><\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">private:<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">\u00a0 int m_nCount;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">};<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5c31\u50cf\u4e0b\u9762\u6f14\u793a\u90a3\u6837\uff0c\u60a8\u53ef\u628a<span style=\"font-family: Courier New;\">Count<\/span>\u201c\u5c5e\u6027\u201d\u5f53\u6210\u662f\u4e00\u4e2a\u666e\u901a\u6210\u5458\u53d8\u91cf\u4e00\u6837\u6765\u4f7f\u7528\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">int i = 5,j;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">PropTest test;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">test.Count = i;\u00a0\u00a0\u00a0 \/\/&#8211; call the set method &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">j= test.Count;\u00a0\u00a0\u00a0\u00a0 \/\/&#8211; call the get method &#8212;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u60a8\u5e0c\u671b\u60a8\u5b9a\u4e49\u7684\u201c\u5c5e\u6027\u201d\u662f\u53ea\u8bfb\u7684\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">property&lt;PropTest,int,READ_ONLY &gt; Count;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u5e0c\u671b\u662f\u53ea\u5199\u7684\uff0c\u5c31\u8fd9\u6837\u505a\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Courier New;\">property&lt;PropTest,int,WRITE_ONLY &gt; Count;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u6ce8\u610f\uff1a\u5982\u679c\u60a8\u628a\u201c\u5c5e\u6027\u201d\u8bbe\u6210\u662f\u53ea\u8bfb\u7684\u800c\u8bd5\u56fe\u53bb\u6539\u5199\u5b83\uff0c\u5c06\u4f1a\u5bfc\u81f4\u4e00\u4e2aassertion\uff08\u65ad\u8a00\uff09\u3002\u5982\u679c\u201c\u5c5e\u6027\u201d\u662f\u53ea\u5199\u7684\u800c\u60a8\u8bd5\u56fe\u53bb\u8bfb\u5b83\uff0c\u4e5f\u4f1a\u53d1\u751f\u540c\u6837\u7684\u60c5\u51b5\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\"><strong>\u603b\u7ed3\uff1a<\/strong><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0cm 0cm 0pt; text-indent: 17.95pt; text-align: justify;\" align=\"center\"><span style=\"font-size: large;\"><span style=\"font-family: \u96b6\u4e66;\"><span style=\"font-family: \u5b8b\u4f53; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';\"><span style=\"font-size: small;\">\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4ec5\u4ec5\u4f7f\u7528\u6807\u51c6C++\u7684\u7279\u6027\u5728C++\u7c7b\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u201c\u5c5e\u6027\u201d\u3002\u5f53\u7136\uff0c\u76f4\u63a5\u8c03\u7528set\/get\u51fd\u6570\u4f1a\u6bd4\u4f7f\u7528\u201c\u5c5e\u6027\u201d\u6548\u7387\u66f4\u9ad8\uff0c\u56e0\u4e3a\u8981\u4f7f\u7528 \u201c\u5c5e\u6027\u201d\uff0c\u60a8\u5c31\u5fc5\u987b\u4e3a\u7c7b\u7684\u6bcf\u4e00\u4e2a\u201c\u5c5e\u6027\u201d\u6765\u5b9e\u4f8b\u5316\u4e00\u4e2a<span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: Courier New;\">property<\/span>\u7c7b\u7684\u5bf9\u8c61\u3002<\/span><\/span><br \/>\n<\/span><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728C++\u4e2d\u5b9e\u73b0\u201c\u5c5e\u6027 (Property)\u201d \u00a0 \u6458\u8981\uff1a \u672c\u6587\u4ecb\u7ecd\u4e86\u5728C++\u4e2d\u5b9e &hellip; <a href=\"https:\/\/www.freemindworld.com\/blog\/2003\/030512_cpp_property.shtml\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,11],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-csdn","category-11"],"_links":{"self":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":0,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}