__declspec(property...)函数象属性那样使用

__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;
    }; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值