Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4540
  • 博文数量: 3
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-30 21:14
文章分类
文章存档

2009年(3)

我的朋友
最近访客

分类: C/C++

2009-04-30 21:30:20

#include
#include

template
class property
{
public:
    property(){pThis = new C;};
    ~property(){delete pThis;}
    T operator=(const T& t)
    {
        return pThis->set(t);
    }
    operator T()
    {
        return pThis->get();
    }
private:
    C*  pThis;
};
//C#的属性
//class TimePeriod
//{
//    private double seconds;
//
//    public double Hours
//    {
//        get { return seconds / 3600; }
//        set { seconds = value * 3600; }
//    }
//}

//C++的模拟
struct TimePeriod
{
    struct Hours
    {
        double get(){ return seconds / 3600; }
        double set(double h){seconds = h * 3600; return h;}
    private:
        double seconds;
    };
    property hours;
};

int main(int, char*[])
{
    TimePeriod c;
    c.hours = 10.1f;
    double h = c.hours;
    printf("%lf\n", h);

    TimePeriod d, e;
    d.hours = e.hours = c.hours;

    printf("%lf\n", (double)d.hours);
    return 0;
}

阅读(469) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~