使用 C++11 的委托构造函数(在初始化列表位置调用),如下:
SensorObjData::SensorObjData() noexcept : sensor_type_{PerceptionTypes::SensorType::UNKNOWN}, sensor_id_{0U},
is_dirty_{false}, cipv_{0U}, num_obstacles_{0U}, num_lane_markers_{0U},
num_traffic_signs_{0U}, num_traffic_lights_{0U},
num_free_space_border_points_{0U}, num_trans_points_{0U},
timestamp_{0}/*, detection_ready_{false}, segmentation_ready_{false}*/
{
data_buf_ptr_ = std::make_shared<std::array<char, kMaxDataLength>>();
}
SensorObjData::SensorObjData(std::int64_t timestamp) noexcept : SensorObjData()
{
timestamp_ = timestamp;
}
总结一下: 1. 委托构造时,初始化列表只能有类本身的构造器 2. 委托构造器的函数体代码(如果有的话)会在委托构造器体运行之前运行。