其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
VC中的“__declspec”能作什么(2)-为类增加属性

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

?????? 属性,是面向对象程序设计中不可缺少的元素,广义的属性是用来描述一个对象所处于的状态。而我们这篇文章所说的属性是狭义的,指能用“=”操作符对类的一个数据进行get或set操作,而且能控制get和set的权限。
??????? 先看一下代码:

#include
#include
#include
#include
using namespace std;
?

class propertytest
{
?int m_xvalue;
?int m_yvalues[100];
?map m_zvalues;
public:
?__declspec(property(get=GetX, put=PutX)) int x;
?__declspec(property(get=GetY, put=PutY)) int y[];
?__declspec(property(get=GetZ, put=PutZ)) int z[];

?int GetX()
?{
??return m_xvalue;
?};
?void PutX(int x)
?{
??m_xvalue = x;
?};
?
?int GetY(int n)
?{
??return m_yvalues[n];
?};

?void PutY(int n,int y)
?{
??m_yvalues[n] = y;
?};

?string GetZ(string key)
?{
??return m_zvalues[key];
?};

?void PutZ(string key,string z)
?{
??m_zvalues[key] = z;
?};

};

int main(int argc, char* argv[])
{
?propertytest test;
?test.x = 3;
?test.y[3] = 4;
?test.z["aaa"] = "aaa";
?std::cout << test.x <?std::cout << test.y[3] <?std::cout << test.z["aaa"] <

?getch();
?return 0;
}
???
?????? 请注意程序中属性的定义方法:

?__declspec(property(get=[get方法名], put=[put方法名])) [类型] [属性名];
??????? __declspec(property)实际上就是做了一个映射,把你的方法映射成属性,以供访问。get和put就是属性访问的权限,一个是读的权限,一个是写的权限。你可以根据需要来决定get和put两种权限的取舍。

相关文章:

VC中的__declspec能作什么-前言

第一章 VC中的“__declspec”能作什么(1)-定义接口

第二章 VC中的“__declspec”能作什么(2)-为类增加属性

?




相关文章

相关软件