c++ passing by const reference

前端 未结 4 2081
独厮守ぢ
独厮守ぢ 2021-01-07 19:23

In the following program body cosists of a vector of pointers. Points is a struct of x,y,z coordinates and a point_id. I believe as body is passed by const reference, the fo

4条回答
  •  自闭症患者
    2021-01-07 19:31

    Here's the issue:

    body.bp[0]->points.push_back(Point_id(p,i));
              ^^
    

    Indirecting through a pointer removes any constness; rather, the constness of the result is dependent on the type of the pointer.

    T *t;              // pointer to T: can modify t and (*t)
    const T *t;        // pointer to const-T: can modify t but not (*t)
    T *const t;        // const-pointer to T: can modify (*t) but not t
    const T *const t;  // const-pointer to const-T: can't modify either t or (*t)
    

提交回复
热议问题