最近在看ros代码,第一次见到这样的类对象初始化。
结构大概是这样的,MapCell 与MapGrid两个类没有继承关系。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)
{}
就是调用MapCell的operator()获取到一个MapGrid然后进行拷贝初始化。