posts - 311, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

指针可以指向基本类型,也可以指向复合类型,因此也可以指向另外一个指针变量,称为指向指针的指针。

int i;
int *pi = &i;
int **ppi = π

这样定义之后,表达式*ppi取pi的值,表达式**ppi取i的值。请读者自己画图理解i、pi、ppi这三个变量之间的关系。

案例:递归查找对象并返回对象指针

bool CustomEntity::getEntityByPart(const String& partID, graphics::IEntity* inEntity, graphics::IEntity** outEntity)
        {
            
//
            if (inEntity && (inEntity->getPartID() == partID))
            {
                
return true;
            }
            
//
            graphics::EntityPtrList::iterator iter;
            
if(inEntity->getSkeleton())        
            {
                graphics::EntityPtrList entityList 
= inEntity->getSkeleton()->getEntities(); 
                
for (iter =  entityList.begin(); iter != entityList.end(); ++ iter)
                {
                    graphics::IEntity
* entity = *iter;                    
                    
if(getEntityByPart(partID, entity, outEntity))
                    {
                        
*outEntity = entity;
                        
return true;
                    }
                }
            }
            
return false;
        }

        
bool CustomEntity::getEntityByPart(const String& partID, graphics::IEntity** outEntity)
        {
            
int num = model_->numComponents();
            
for (int i = 0; i < num; ++i)
            {
                model::ModelComponent 
*mc = model_->getComponentByIndex(i);
                
if (mc->getType() == model::ModelComponent::MCT_GRAPHIC)
                {
                    model::Graphical 
*gc = static_cast<model::Graphical *>(mc);
                    model::Graphical::TagEntityMap::iterator itEntity 
= gc->entities_.begin();
                    model::Graphical::TagEntityMap::iterator itEnd 
= gc->entities_.end();
                    
while (itEntity != itEnd)
                    {
                        graphics::IEntity
* entity = itEntity->second.p_;
                        
if(getEntityByPart(partID, entity, outEntity))
                        {
                            
*outEntity = entity;
                            
return true;
                        }
                        
++itEntity;
                    }
                }
            }
            
return false;
        }