2010.11.16 DYNAMIC_DOWNCAST用法

本文介绍了一段可能存在问题的代码,该代码尝试从活动视图中获取特定类型的列表控件指针。为了避免潜在的问题,文章推荐使用DYNAMIC_DOWNCAST宏来安全地转换指针类型,并检查转换是否合法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下这段儿代码
CPnrListCtrl& listCtrl=((CPnrListView*)GetActiveView())->GetListCtrl();
有可能会有问题。就是当GetActiveView()返回的不是CPnrListView的时候,得到的listCtrl是错误的指针(也可能指向未知位置)

如下可以解决
CPnrListView *pListView=DYNAMIC_DOWNCAST(CPnrListView,GetActiveView());
 if(pListView==NULL)
  return;

 

 

 

Provides a handy way to cast a pointer to a pointer to a class object while checking to see if the cast is legal.

DYNAMIC_DOWNCAST(class, pointer )
class

The name of a class.

pointer

A pointer to be cast to a pointer to an object of type class.

The macro will cast the pointer parameter to a pointer to an object of the class parameter's type.

If the object referenced by the pointer is a "kind of" the identified class, the macro returns the appropriate pointer. If it is not a legal cast, the macro returns NULL.

 

转载于:https://www.cnblogs.com/peijihui/archive/2010/11/17/1879542.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值