zoukankan      html  css  js  c++  java
  • C++实现C#的get,set属性操作

    原文地址:http://www.codeproject.com/KB/cpp/properties.aspx
    // properties.h
    
    #ifndef _PROPERTIES_H
    #define _PROPERTIES_H
    
    #define PROPERTY(t,n)  __declspec( property ( put = property__set_##n, get = property__get_##n ) ) t n;/
    	typedef t property__tmp_type_##n
    #define READONLY_PROPERTY(t,n) __declspec( property (get = property__get_##n) ) t n;/
    	typedef t property__tmp_type_##n
    #define WRITEONLY_PROPERTY(t,n) __declspec( property (put = property__set_##n) ) t n;/
    	typedef t property__tmp_type_##n
    
    #define GET(n) property__tmp_type_##n property__get_##n()
    #define SET(n) void property__set_##n(const property__tmp_type_##n& value)
    
    #endif /* _PROPERTIES_H */ 
     
    // main.cpp
    #include <iostream>
    
    
    #include <math.h>
    
    
    #include "properties.h"
    
    
    
    class Vector2
    {
    public:
    	float x;
    	float y;
    
    	READONLY_PROPERTY(float, Length);
    	GET(Length) 
    	{ 
    		return sqrt((x*x + y*y));
    	}
    };
    
    int main()
    {
    	Vector2 vec;
    	vec.x = 1;
    	vec.y = 1;
    	std::cout << "Length of vector(" << vec.x << ", " << vec.y << ") = ";
    	std::cout << vec.Length << "/n"; // <--- property, not a function call
    
    	return 0;
    }  
     
  • 相关阅读:
    web安全
    WCF通信架构
    WCF地址
    WCFContracts(契约)
    分布式中的王者WCF
    SOAP 介绍
    诊所管理软件
    MFC 画图CDC双缓冲
    Linux 启动、关闭、重启网络服务
    C# 除法的小数点问题
  • 原文地址:https://www.cnblogs.com/marryZhan/p/2213936.html
Copyright © 2011-2022 走看看