__declspec(property...)编译指示符是微软定义的一个针对C语言的扩展,使一个函数象一个属性那样被使用。这样你就可以采用如下的语法形式象在使用VB一样读写一个属性的值: objectPtr->property =
value;
定义如下:
declarator __declspec(
property( get=get_func_name ) )
declarator
__declspec( property( put=put_func_name ) )
declarator
__declspec( property( get=get_func_name, put=put_func_name ) ) declarator
当编译器在成员选择运算符(“.”或“->”)的右侧看到使用此特性声明的数据成员时,它会根据此类表达式是左值还是右值,将运算转换为 get 或 put 函数。 在更复杂的上下文中(如“+=”),可通过同时执行 get 和
put 来执行重写。
__declspec(property...)编译指示符只能针对属性的读写函数使用,并根据属性是否可供读写自动生成对应的调用形式。每个属性可能有GetProperty,
PutProperty,PutRefProperty三个函数,但这个编译符只能生成其中的两种交互形式。比如,Command对象的ActiveConnection属性有GetActiveConnection和PutRefActiveConnection这两个读写函数。而PutRef-的形式在实践中是个好的选择,你可以将一个活动的Connection对象的指针保存在这个属性中。另一方面,Recordset对象则有Get-, Put-, and PutRefActiveConnection操作,但却没有可交互的语法形式。
示例:
class A
{
private:
int m_Value;
public:
int get_Value() { return
m_Value; }
void set_Value( int value ){
m_Value = value; }
__declspec( property( get =
get_Value, put = set_Value ) ) int Value;
};
void main()
{
A a;
a.Value = 10;
printf( "Value = %d\n", a.Value
);
a.set_Value( 100 );
printf( "Value = %d\n",
a.get_Value() );
}
输出:10,100
__declspec(property(get = GetINotify)) IEmulatorMgr::INotify* N;
IEmulatorMgr::INotify* m_pINotify;
IEmulatorMgr::INotify* GetINotify(void)
{
return m_pINotify;
};