在C++中实现“属性 (Property)”

摘要:

 
本文介绍了在C++中实现“属性 (Property)”的方法,“属性”是我们在C#(或其它一些语言)中常常能用到的一种特性。这里介绍的实现方法使用的是标准的C++,没有用任何其它的语言扩展。而大部分的库或是编译器为了实现“属性”,往往对C++作一些扩展,就像我们在托管的C++或是C++ Builder中看到的那样,也有的是使用普通的set和get方法,这些都不能算是真正的“属性”。
 
 
 
正文:
 
首先,让我们来看看什么是“属性”。“属性”在外观上看起来就像类中的一个普通成员变量(或者称为是“字段”),但它内部是通过一组set/get方法(或称作read/write方法)来访问类中实际的成员变量。
 
举例来说,如果我有一个类A和它的一个“属性”Count,我就可以写出如下的代码:
 
A foo;
 
cout << foo.Count;
 
Count实际上是调用了一个get函数,并返回了我们所希望得到的成员变量值。使用“属性”而不是直接使用成员变量值的最大好处是你可控制这个“属性”是只读的(您只能读出它的值而不能改变它的值)、只写的、或是可读可写的。让我们一起来实现它吧:
 
我们希望能实现下面的用法:
 
int i = foo.Count; //-- 实际上调用get函数来获取实际的成员变量的值 --
 
foo.Count = i;   //-- 实际上将调用set函数来设置实际的成员变量的值 --
 
因此,很明显的,我们需要重载“=”运算符以便可以设置“属性”的值,还要正确处理“属性”的返回类型(稍后就可以看到一点)。
 
我们将实现一个类,名叫property,它将表现得像一个“属性”,它的结构如下:
 
template<typename Container, typename ValueType, int nPropType>
 
class property {}
 
这个类模版将表现为我们需要的“属性”。Container是一个类的类型(后面我们称之为“容量类”),这个类就是包含要实现为“属性”的实际成员变量、访问这个变量的set/get方法和表现出来的“属性”的类。ValueType是容量类内部的实际成员变量的类型(也将成为“属性”的类型),nPropType表示“属性”的类别:“只读”、“只写”或是“读写”。
 
我们还需要设置一组指针,指向容器类特定成员变量的set和get方法,同时还要重载“=”运算符,使得“属性”可以表现得像一个变量。下面我们看看property类的完整程序。
 
#define READ_ONLY 1
 
#define WRITE_ONLY 2
 
#define READ_WRITE 3
 
 
 
template <typename Container, typename ValueType, int nPropType>
 
class property
 
{
 
public:
 
property()
 
{
 
  m_cObject = NULL;
 
  Set = NULL;
 
  Get = NULL;
 
}
 
//-- This to set a pointer to the class that contain the
 
//   property --
 
void setContainer(Container* cObject)
 
{
 
  m_cObject = cObject;
 
}
 
//-- Set the set member function that will change the value --
 
void setter(void (Container::*pSet)(ValueType value))
 
{
 
  if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE))
 
    Set = pSet;
 
  else
 
    Set = NULL;
 
}
 
//-- Set the get member function that will retrieve the value --
 
void getter(ValueType (Container::*pGet)())
 
{
 
  if((nPropType == READ_ONLY) || (nPropType == READ_WRITE))
 
    Get = pGet;
 
  else
 
    Get = NULL;
 
}
 
//-- Overload the '=' sign to set the value using the set
 
//   member --
 
ValueType operator =(const ValueType& value)
 
{
 
  assert(m_cObject != NULL);
 
  assert(Set != NULL);
 
  (m_cObject->*Set)(value);
 
  return value;
 
}
 
//-- To make possible to cast the property class to the
 
//   internal type --
 
operator ValueType()
 
{
 
  assert(m_cObject != NULL);
 
  assert(Get != NULL);
 
  return (m_cObject->*Get)();
 
}
 
private:
 
  Container* m_cObject;  //-- Pointer to the module that
 
                         //   contains the property --
 
  void (Container::*Set)(ValueType value);
 
                         //-- Pointer to set member function --
 
  ValueType (Container::*Get)();
 
                         //-- Pointer to get member function --
 
};
 
 
 
让我们来一段段的分析程序:
 
下面这段代码把Container指针指向一个有效的对象,这个对象就是我们要添加“属性”的类(也就是容器类)的对象。
 
void setContainer(Container * cObject)
 
 
  m_cObject = cObject;
 
}
 
下面这段代码,设置指针指向容器类的set/get成员函数。这里仅有的一点限制是set函数必须是带一个参数且返回void的函数,而get函数必须不带参数且返回ValueType型的值。
 
//-- Set the set member function that will change the value --
 
void setter(void (Container::*pSet)(ValueType value))
 
{
 
  if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE))
 
    Set = pSet;
 
  else
 
    Set = NULL;
 
}
 
//-- Set the get member function that will retrieve the value --
 
void getter(ValueType (Container::*pGet)())
 
{
 
  if((nPropType == READ_ONLY) || (nPropType == READ_WRITE))
 
    Get = pGet;
 
  else
 
    Get = NULL;
 
}
 
下面这段代码,首先是对“=”运算符进行了重载,它调用了容器类的set成员函数以实现赋值操作。然后是定义了一个转换函数,它返回get函数的返回值,这使整个property类表现得像一个ValueType型的成员变量。
 
//-- Overload the '=' sign to set the value using the set member --
 
ValueType operator =(const ValueType& value)
 
{
 
  assert(m_cObject != NULL);
 
  assert(Set != NULL);
 
  (m_cObject->*Set)(value);
 
  return value;
 
}
 
//-- To make possible to cast the property class to the
 
//   internal type --
 
operator ValueType()
 
{
 
  assert(m_cObject != NULL);
 
  assert(Get != NULL);
 
  return (m_cObject->*Get)();
 
}
 
 
 
下面让我们看看我们是如何来使用这个property类的:
 
就像下面的代码中所展示的一样:PropTest类实现了一个名为Count的“属性”。这个“属性”的值实际上是通过get函数从一个名为m_nCount的私有变量获得并通过set函数把这个“属性”值的变动写回到m_nCount中去的。get/set函数可以任意命名,因为它们是通过它们的函数地址传递给property类的,就像您在PropTest类的构造函数中看到的那样。PropTest类中的"property<PropTest,int,READ_WRITE> Count; "一行就使我们的PropTest类就拥有了一个名叫Count可以被读写的整型“属性”了。您可以把Count当成是一个普通的成员变量一样来使用,而实际上,对Count的读写都是通过set/get函数间接的实现的。
 
PropTest类的构造函数中所做的初始化工作是必须的,只有这样才能保证定义的“属性”可以正常的工作。
 
class PropTest
 
{
 
public:
 
  PropTest()
 
  {
 
    Count.setContainer(this);
 
    Count.setter(&PropTest::setCount);
 
    Count.getter(&PropTest::getCount);
 
  }
 
  int getCount()
 
  {
 
    return m_nCount;
 
  }
 
  void setCount(int nCount)
 
  {
 
    m_nCount = nCount;
 
  }
 
  property<PropTest,int,READ_WRITE> Count;
 
 
 
 
 
private:
 
  int m_nCount;
 
};
 
 
 
就像下面演示那样,您可把Count“属性”当成是一个普通成员变量一样来使用:
 
int i = 5,j;
 
PropTest test;
 
test.Count = i;    //-- call the set method --
 
j= test.Count;     //-- call the get method --
 
如果您希望您定义的“属性”是只读的,您可以这样做:
 
property<PropTest,int,READ_ONLY > Count;
 
如果希望是只写的,就这样做:
 
property<PropTest,int,WRITE_ONLY > Count;
 
注意:如果您把“属性”设成是只读的而试图去改写它,将会导致一个assertion(断言)。如果“属性”是只写的而您试图去读它,也会发生同样的情况。
 
 
 
总结:
 
本文介绍了如何仅仅使用标准C++的特性在C++类中实现一个“属性”。当然,直接调用set/get函数会比使用“属性”效率更高,因为要使用 “属性”,您就必须为类的每一个“属性”来实例化一个property类的对象。
posted @ 2017-10-08 12:24  贞诚善佑  阅读(5490)  评论(0编辑  收藏  举报