C++ 如何支持属性

                                                C++ 如何支持属性

 

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

       

先看一下代码:

 #include <IOSTREAM>

#include <map>

#include <string>

#include <CONIO.H>

using namespace std;

 

class propertytest

{

    int m_xvalue;

    int m_yvalues[100];

    map<string,string> 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[];   //应该是string 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::endl;

    std::cout << test.y[3] <<std::endl;

    std::cout << test.z["aaa"] <<std::endl;

 

    getch();

    return 0;

}

 

 Reference:

http://blog.youkuaiyun.com/wqf2/archive/2004/08/20/79977.aspx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值