{"id":114,"date":"2003-05-18T11:00:00","date_gmt":"2003-05-18T03:00:00","guid":{"rendered":"http:\/\/www.freemindworld.com\/blog\/2003\/05\/0518_visual-c%e7%9a%84%e8%99%9a%e6%8b%9f%e5%b1%9e%e6%80%a7%e5%8a%9f%e8%83%bd.shtml"},"modified":"2012-01-30T18:05:18","modified_gmt":"2012-01-30T10:05:18","slug":"030518_vc_vproperty","status":"publish","type":"post","link":"https:\/\/www.freemindworld.com\/blog\/2003\/030518_vc_vproperty.shtml","title":{"rendered":"[\u7ffb\u8bd1] Visual C++\u7684\u201c\u865a\u62df\u5c5e\u6027\u201d\u529f\u80fd"},"content":{"rendered":"<p align=\"center\"><span style=\"font-size: medium;\">Microsoft Visual C++\u7684\u201c\u865a\u62df\u5c5e\u6027\u201d\u529f\u80fd<\/span><\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a<\/strong><\/p>\n<p>\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e86\u4f7f\u7528Microsoft Visual C++\u4e2d\u7684__declspec\u5173\u952e\u5b57\u6765\u5b9e\u73b0\u201c\u5c5e\u6027\uff08Property\uff09\u201d\u8fd9\u4e2aC++\u4e2d\u6ca1\u6709\u7684\u7279\u6027\u7684\u65b9\u6cd5\u3002\u6709\u5173__declspec\u5173\u952e\u5b57\u7684\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u53c2\u8003MSDN\u3002<\/p>\n<p>__declspec\u5173\u952e\u5b57\u4e0d\u662f\u6807\u51c6C++\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u8fd9\u79cd\u5b9e\u73b0\u201c\u5c5e\u6027\u201d\u7684\u65b9\u6cd5\u53ea\u9002\u7528\u4e8eVisual C++\uff0c\u5982\u679c\u60f3\u8981\u4e86\u89e3\u5728\u6807\u51c6C++\u4e2d\u6a21\u62df\u5b9e\u73b0\u201c\u5c5e\u6027\u201d\u7684\u65b9\u6cd5\uff0c\u8bf7\u53c2\u8003\uff1a<\/p>\n<p><a href=\"http:\/\/www.csdn.net\/develop\/read_article.asp?id=18361\">http:\/\/www.csdn.net\/develop\/read_article.asp?id=18361<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6b63\u6587\uff1a<\/strong><\/p>\n<p>\u5f88\u591a\u9057\u7559\u4e0b\u6765\u7684\u4f20\u7edfC++\u4ee3\u7801\u4e2d\u5e38\u5e38\u4f1a\u51fa\u73b0\u7528public\u6216protected\u5173\u952e\u5b57\u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u53bb\u8bbf\u95ee\u5b83\u4eec\uff08\u8bd1\u8005\u6ce8\uff0c\u5982\u679c\u662fprotected\uff0c\u662f\u6307\u53ef\u4ee5\u5728\u5176\u6d3e\u751f\u7c7b\u4e2d\u76f4\u63a5\u8bbf\u95ee\uff09\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u4e00\u7ec4\u7b80\u5355\u7684get\/set\u65b9\u6cd5\u3002\u4e3e\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c\u5982\u4e0b\u7684\u7ed3\u6784\u5b9a\u4e49\u5c31\u662f\u8fd9\u6837\u7684\u60c5\u51b5\uff1a<\/p>\n<p>typedef struct tagMyStruct<br \/>\n{<br \/>\nlong m_lValue1;<br \/>\n&#8230;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Rest of the structure definition.<br \/>\n} SMyStruct;<\/p>\n<p>\u5728\u4f7f\u7528\u8fd9\u4e2a\u7ed3\u6784\u4f53\u7684\u5ba2\u6237\u7aef\u7a0b\u5e8f\u4e2d\u5c31\u53ef\u4ee5\u770b\u5230\u6563\u5e03\u7740\u5927\u91cf\u7c7b\u5982\u4e0b\u9762\u5217\u51fa\u7684\u4ee3\u7801\uff1a<\/p>\n<p>SMyStruct\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MyStruct;<br \/>\nlong\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lTempValue;<\/p>\n<p>MyStruct.m_lValue1 = 100;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Or any other value that is to be assigned to it.<br \/>\n&#8230;<br \/>\nlTempValue = MyStruct.m_lValue1;<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e00\u65e6\u8fd9\u6bb5\u4ee3\u7801\u9700\u8981\u5728\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7684\u73af\u5883\u4e0b\u5e94\u7528\uff0c\u4f60\u5c31\u4f1a\u9047\u5230\u4e00\u4e2a\u9ebb\u70e6\u3002\u56e0\u4e3a\u6ca1\u6709get\/set\u65b9\u6cd5\u7684\u5b58\u5728\uff0c\u4f60\u4e0d\u53ef\u80fd\u7b80\u5355\u7684\u5728SMyStruct\u7684\u5b9a\u4e49\u4e2d\u52a0\u4e0a\u4e00\u4e2a\u4e34\u754c\u533a\uff08\u6216\u4e92\u65a5\u91cf\uff09\u6765\u4fdd\u62a4\u5305\u62ecm_lValue1\u5728\u5185\u7684\u6240\u6709\u516c\u6709\u6210\u5458\u53d8\u91cf\u3002<\/p>\n<p>\u5982\u679c\u60a8\u662f\u4f7f\u7528Microsoft Visual C++\u7f16\u8bd1\u5668\uff0c\u60a8\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u627e\u5230\u4e00\u4e2a\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6848\u3002<\/p>\n<p>\u60a8\u53ea\u9700\u628a\u60a8\u7684\u7ed3\u6784\u4f53\u91cd\u5199\u4e3a\u5982\u4e0b\u7684\u5f62\u5f0f\uff1a<\/p>\n<p>typedef struct tagMyStruct<br \/>\n{<br \/>\n__declspec(property(get=GetValue1, put=PutValue1))<br \/>\nlong\u00a0 m_lValue1;<br \/>\n&#8230;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Rest of the structure definition.<br \/>\nlong GetValue1()<br \/>\n{<br \/>\n\/\/ Lock critical section<br \/>\nreturn m_lInternalValue1;<br \/>\n\/\/ Unlock critical section.<br \/>\n}<br \/>\nvoid PutValue1(long lValue)<br \/>\n{<br \/>\n\/\/ Lock critical section<br \/>\nm_lInternalValue1 = lValue;<br \/>\n\/\/ Unlock critical section<br \/>\n}<br \/>\nprivate:<br \/>\nlong m_lInternalValue1;<br \/>\n\/\/ Define critical section member variable.<br \/>\n} SMyStruct;<\/p>\n<p>\u8fd9\u5c31\u662f\u60a8\u8981\u505a\u7684\u5168\u90e8\uff01<\/p>\n<p>\u5728\u8fd9\u4ee5\u540e\uff0c\u5bf9\u4e8e\u5982\u4e0b\u7684\u4ee3\u7801\uff1a<\/p>\n<p>MyStruct.m_lValue1 = 100<\/p>\n<p>\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\uff1a<\/p>\n<p>MyStruct.PutValue(100)<\/p>\n<p>\u5bf9\u4e8e\u5982\u4e0b\u7684\u4ee3\u7801\uff1a<\/p>\n<p>lTempValue = MyStruct.m_lValue1<\/p>\n<p>\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\uff1a<\/p>\n<p>lTempValue = MyStruct.GetValuel()<\/p>\n<p>\u8fd9\u6837\u7684\u7279\u6027\u80fd\u5e26\u6765\u5f88\u591a\u6709\u7528\u7684\u529f\u80fd\uff0c\u60a8\u751a\u81f3\u53ef\u4ee5\u7528\u5b83\u4e3a\u60a8\u539f\u6765\u7684\u7ed3\u6784\u4f53\u6216\u7c7b\u52a0\u4e0a\u5f15\u7528\u8ba1\u6570\u7684\u529f\u80fd\uff01<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8bd1\u8005\u8865\u5145\uff1a<\/strong><\/p>\n<p>\u5bf9\u4e8e\u7c7b\u5982\u6570\u7ec4\u7684\u60c5\u51b5\uff0cVC\u4e5f\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u652f\u6301\uff0c\u5982\u4e0b\u7684\u4f8b\u5b50\uff1a<\/p>\n<p align=\"left\">#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>class MyStruct<br \/>\n{<br \/>\npublic:<br \/>\n__declspec(property(get=GetValue1, put=PutValue1))<br \/>\nint t[][]; \/\/<span style=\"font-family: \u5b8b\u4f53;\">\u4ee5\u4e8c\u7ef4\u6570\u7ec4\u6765\u6f14\u793a<\/span><br \/>\nint GetValue1(int x, int y) \/\/x,y<span style=\"font-family: \u5b8b\u4f53;\">\u5206\u522b\u5bf9\u5e94\u7b2c\u4e00\u7ef4\u548c\u7b2c\u4e8c\u7ef4\u7684\u4e0b\u6807<\/span><br \/>\n{<br \/>\nreturn m_lInternalValue1[x][y];<br \/>\n}<br \/>\nvoid PutValue1(int x,int y, int lValue) \/\/x,y<span style=\"font-family: \u5b8b\u4f53;\"><span style=\"font-family: \u5b8b\u4f53;\">\u5206\u522b\u5bf9\u5e94\u7b2c\u4e00\u7ef4\u548c\u7b2c\u4e8c\u7ef4\u7684\u4e0b\u6807\uff0c<\/span>lValue<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e3a\u8981\u8d4b\u7684\u503c<br \/>\n<\/span>\u00a0\u00a0 {<br \/>\nm_lInternalValue1[x][y] = lValue;<br \/>\n}<br \/>\nprivate:<br \/>\nint m_lInternalValue1[3][3];<\/p>\n<p>};<\/p>\n<p>int main()<br \/>\n{<br \/>\nMyStruct ms;<br \/>\nfor (int i = 0; i &lt; 3; i++)<br \/>\nfor (int j = 0; j &lt; 3; j++)<br \/>\nms.t[i][j] = i * j;<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p><span style=\"font-family: \u5b8b\u4f53;\">\u5728<\/span>VC6<span style=\"font-family: \u5b8b\u4f53;\">\u548c<\/span>VC7<span style=\"font-family: \u5b8b\u4f53;\">\u4e2d\uff0c\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\u7684\u5904\u7406\u7565\u6709\u4e0d\u540c\uff0c\u5982\u4e0a\u9762\u7684<\/span><\/p>\n<p align=\"left\">\u00a0\u00a0 __declspec(property(get=GetValue1, put=PutValue1))<br \/>\nint t[][];<\/p>\n<p>\u5728VC6\u4e2d\u53ef\u4ee5\u7b80\u5355\u7684\u5199\u6210int t[];\u5373\u53ef\u652f\u6301\u4e24\u7ef4\u7684\u6570\u7ec4\uff0c\u800c\u5728VC7\u4e2d\u5fc5\u987b\u5199\u6210int t[][];\u624d\u53ef\u4ee5\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u539f\u6587\u9009\u81ea\uff1a<a href=\"http:\/\/www.codeproject.com\/cpp\/virtual_property.asp\">http:\/\/www.codeproject.com\/cpp\/virtual_property.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft Visual C++\u7684\u201c\u865a\u62df\u5c5e\u6027\u201d\u529f\u80fd \u8bd1\u8005\u6ce8\uff1a \u672c\u6587\u7b80\u5355\u4ecb &hellip; <a href=\"https:\/\/www.freemindworld.com\/blog\/2003\/030518_vc_vproperty.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-114","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\/114","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=114"}],"version-history":[{"count":0,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}