2010.11.16 DYNAMIC_DOWNCAST用法

以下这段儿代码
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.

 

posted @ 2010-11-17 09:37  pjh123  阅读(1024)  评论(0编辑  收藏  举报