C++通过智能指针向量迭代

lam*_*bda 10 c++ stl smart-pointers vector

我有一个具有此功能的类:

typedef boost::shared_ptr<PrimShapeBase> sp_PrimShapeBase; 



class Control{
     public:
         //other functions
         RenderVectors(SDL_Surface*destination, sp_PrimShapeBase);
     private:
         //other vars
          vector<sp_PrimShapeBase> LineVector;

};

//the problem of the program

void Control::RenderVectors(SDL_Surface*destination, sp_PrimShapeBase){
    vector<sp_PrimShapeBase>::iterator i;

    //iterate through the vector
    for(i = LineVector.begin(); i != LineVector.end(); i ++ ){
      //access a certain function of the class PrimShapeBase through the smart
      //pointers
      (i)->RenderShape(destination); 

    }
}
Run Code Online (Sandbox Code Playgroud)

编译器告诉我类boost :: shared_ptr没有名为'RenderShape'的成员,我觉得这很奇怪,因为类PrimShapeBase肯定有这个功能但是在不同的头文件中.这是什么原因?

Luc*_*ore 19

不是吗?

(*i)->RenderShape(destination); 
Run Code Online (Sandbox Code Playgroud)

i是迭代器,*ishared_ptr,(*i)::operator->()是对象.


Wug*_*Wug 7

那是因为它i是一个迭代器.取消引用它会给你一个智能指针,你需要双重取消引用它.

(**i).RenderShape(destination);
Run Code Online (Sandbox Code Playgroud)

要么

(*i)->RenderShape(destination); 
Run Code Online (Sandbox Code Playgroud)

  • `++ i ++ ----&gt; derp(); // wat` (2认同)