鍍金池/ 問答/C++/ c++ 類對象初始化

c++ 類對象初始化

最近在看ros代碼,第一次見到這樣的類對象初始化。
結(jié)構(gòu)大概是這樣的,MapCell 與MapGrid兩個類沒有繼承關(guān)系。trajetory_planner中MapCell對象的初始化是怎么進行的?

trajetory_planner.h

class TrajectoryPlanner{
    friend class TrajectoryPlannerTest;
    public:
    ...
    private:
    ...
        MapGrid path_map_;
}

trajetory_planner.cpp

...
MapCell cell = path_map_(cx, cy);//這里是怎么初始化的
if (cell.within_robot) {
        return false;
    }
...

map_grid.h

class MapGrid{
public:
    MapGrid();
    MapGrid(unsigned int size_x, unsigned int size_y);
    ...
    inline MapCell& operator() (unsigned int x, unsigned int y){
        return map_[size_x_ * y + x];
    }
    inline MapCell operator() (unsigned int x, unsigned int y) const {
        return map_[size_x_ * y + x];
    }
private:
      std::vector<MapCell> map_;
}

map_cell.h

class MapCell{
public:
    MapCell();
    MapCell(const MapCell& mc);
    unsigned int cx, cy; 
    double target_dist; 
    bool target_mark;
    bool within_robot;
}

map_cell.cpp

MapCell::MapCell()
    : cx(0), cy(0),
      target_dist(DBL_MAX),
      target_mark(false),
      within_robot(false)
  {}
MapCell::MapCell(const MapCell& mc)
    : cx(mc.cx), cy(mc.cy),
      target_dist(mc.target_dist),
      target_mark(mc.target_mark),
      within_robot(mc.within_robot)
  {}
回答
編輯回答
怪痞

就是調(diào)用MapCell的operator()獲取到一個MapGrid然后進行拷貝初始化。

2017年11月7日 05:48