C++实现属性源代码(借鉴,模仿)

//property.h #pragma once #include <assert.h> int const READ_ONLY = 1; int const WRITE_ONLY = 2; int const READ_WRITE = 4; template<class CContainer, class CPropType, int nAccessType> class CProperty { public: CProperty() { m_pobjContain = NULL; m_pfunSet = NULL; m_pfunGet = NULL; } ~CProperty() { } public: void SetContainer(CContainer* pContain) { m_pobjContain = pContain; } void SetSetFunction(void (CContainer::*pSet)(CPropType val) ) { if ( (WRITE_ONLY == nAccessType) || (READ_WRITE == nAccessType) ) { m_pfunSet = pSet; } else { m_pfunSet = NULL; } } void SetGetFunction(CPropType (CContainer::*pGet)() ) { if ( (READ_ONLY == nAccessType) || (READ_WRITE == nAccessType) ) { m_pfunGet = pGet; } else { m_pfunGet = NULL; } } CPropType operator = (const CPropType& val) { assert(NULL != m_pobjContain); assert(NULL != m_pfunSet); (m_pobjContain->*m_pfunSet)(val); return val; } operator CPropType() { assert(NULL != m_pobjContain); assert(NULL != m_pfunGet); return (m_pobjContain->*m_pfunGet)(); } private: CContainer* m_pobjContain; void (CContainer::*m_pfunSet)(CPropType val); CPropType (CContainer::*m_pfunGet)(); }; //main.cpp // UseProp.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Property.h" #include <iostream> using namespace std; class CPropTest { public: CPropTest(int nVal = 0) { m_nData = nVal; m_nVal.SetContainer(this); m_nVal.SetSetFunction(&CPropTest::SetData); m_nVal.SetGetFunction(&CPropTest::GetData); } ~CPropTest() { } public: CProperty<CPropTest, int, READ_WRITE> m_nVal; int GetData() { return m_nData; } void SetData(int nVal) { m_nData = nVal; } private: int m_nData; }; int main() { CPropTest obj; obj.m_nVal = 10; int nVal = obj.m_nVal; cout << obj.m_nVal << endl; return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++实现属性 本文译自http://www.codeguru.com/cpp_mfc/Property.html的Implementing a Property in C++ 以下是译文 本文由Emad Barsoum投稿。 开发测试环境:Visual C++ 7.0, Windows XP sp1, Windows 2000 sp3 摘要 本文试着在C++不使用任何扩展技术模拟C#(或其他语言)属性特征。大多数在C++实现属性的库和编译器使用扩展技术,如Managed C++C++ Builder,或者他们使用如通常函数的set和get方法,但那不是属性。 详述 我们首先看一下什么是属性。一个属性表现为一个字段或者成员变量,但它通过read和write方法或者get和set方法暗操作变量。 例如,若存在类A和它的属性Count,我可以写如下的代码: A foo; Cout << foo.Count; 实际上Count调用它的get函数返回当前的变量值。你可以将属性定为只读(你可以读取它但不能修改它)、只写或者可读写,这就是使用属性而不直接使用变量的的一个最大好处了。好了,让我们开始来实现它: 我们需要能做如下的事: int i = foo.Count; //--调用get函数得到值 foo.Count = i; //-- 调用set函数设定值 因此,很明显的我们需要重载 = 操作符使其能设定变量的值,同时也要重载该属性的返回值(在下面我们将会看到的)。 我们将实现一个称为property的类,它做的就像一个属性,声明如下: template class property {} 这个模板类表示的是我们的属性。Container是我们要在其包含属性的类变量,set和get方法以及属性的类的类型。ValueType是内部变量即要定义的属性的类型,nPropType定义属性的读写标志:只读、只写或可读写。 现在我们需要一个指向从包含属性的类Container到属性类property的set和get方法的指针,同时重载 = 操作符以使得属性能象变量起那样作用。现在我们来看property类的全部定义 #define READ_ONLY 1 #define WRITE_ONLY 2 #define READ_WRITE 3 template class property { public: property() { m_cObject = NULL; Set = NULL; Get = NULL; } //-- 将m_cObject指向包含属性的container类 -- void setContainer(Container* cObject) { m_cObject = cObject; } //-- 设定可改变属性值的set成员函数 -- void setter(void (Container::*pSet)(ValueType value)) { if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE)) Set = pSet; else Set = NULL; } //-- 设定可检索属性值的get成员函数 -- void getter(ValueType (Container::*pGet)()) { if((nPropType == READ_ONLY) || (nPropType == READ_WRITE)) Get = pGet; else Get = NULL; } //-- 重载 = 号操作符使其能用set成员设定属性值-- ValueType operator =(const ValueType& value) { assert(m_cObject != NULL); assert(Set != NULL); (m_cObject->*Set)(value); return value; } //-- 使属性类能转换为内部类型成为可能-- operator ValueType() { assert(m_cObject != NULL); assert(Get != NULL);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值