虚函数作用:
通过基类指针只能访问派生类的成员变量,但是不能访问派生类的成员函数。为了让基类指针能够访问派生类的成员函数,C++ 增加了虚函数(Virtual Function)。使用虚函数非常简单,只需要在函数声明前面增加 virtual 关键字。
虚函数的使用要点:
- 只需要在虚函数的声明处加上 virtual 关键字,函数定义处可以加也可以不加。
- 为了方便,你可以只将基类中的函数声明为虚函数,这样所有派生类中具有遮蔽关系的同名函数都将自动成为虚函数。
- 当在基类中定义了虚函数时,如果派生类没有定义新的函数来遮蔽此函数,那么将使用基类的虚函数。
- 只有派生类的虚函数覆盖基类的虚函数(函数原型相同)才能构成多态(通过基类指针访问派生类函数)。
- 构造函数不能是虚函数。
- 析构函数可以声明为虚函数,而且有时候必须要声明为虚函数。
多态
有了虚函数,基类指针指向基类对象时就使用基类的成员(包括成员函数和成员变量),指向派生类对象时就使用派生类的成员。换句话说,基类指针可以按照基类的方式来做事,也可以按照派生类的方式来做事,它有多种形态,或者说有多种表现方式,称为多态(Polymorphism)。
多态作用:
- 可以通过基类指针对所有派生类(包括直接派生和间接派生)的成员变量和成员函数进行“全方位”的访问,尤其是成员函数。如果没有多态,我们只能访问成员变量。
- 不使用多态,那么就可能需要定义多个指针变量,很容易造成混乱;而有了多态,只需要一个指针变量 p 就可以调用所有派生类的虚函数。
注: 借助引用也可以实现多态,因为引用在本质上是通过指针的方式实现的;不过引用不像指针灵活,指针可以随时改变指向,而引用只能指代固定的对象,在多态性方面缺乏灵活性。
构成多态的条件:
- 必须存在继承关系
- 继承关系中必须有同名的虚函数,并且它们是覆盖关系(函数原型相同)
- 存在基类的指针,通过该指针调用虚函数
声明虚函数用途:
首先看成员函数所在的类是否会作为基类。然后看成员函数在类的继承后有无可能被更改功能,如果希望更改其功能的,一般应该将它声明为虚函数。如果成员函数在类被继承后功能不需修改,或派生类用不到该函数,则不要把它声明为虚函数。
纯虚函数与抽象类
纯虚函数,语法格式为:
virtual 返回值类型 函数名 (函数参数) = 0;
纯虚函数没有函数体,只有函数声明,在虚函数声明的结尾加上=0,表明此函数为纯虚函数。
最后的=0并不表示函数返回值为0,它只起形式上的作用,告诉编译系统“这是纯虚函数”。
包含纯虚函数的类称为抽象类(Abstract Class)。之所以说它抽象,是因为它无法实例化,也就是无法创建对象。原因很明显,纯虚函数没有函数体,不是完整的函数,无法调用,也无法为其分配内存空间。
抽象类通常是作为基类,让派生类去实现纯虚函数。派生类必须实现纯虚函数才能被实例化。
实例:
基类虚函数及纯虚函数定义:
world.h
#include <vector>
#include <costmap_2d/observation.h>
#include <costmap_2d/footprint.h>
#include <geometry_msgs/Point.h>
namespace base_local_planner {
// WorldModel是一个基类并且也是一个抽象类
class WorldModel{
public:
virtual double footprintCost(const geometry_msgs::Point& position, const std::vector<geometry_msgs::Point>& footprint,
double inscribed_radius, double circumscribed_radius) = 0; // 纯虚函数
double footprintCost(double x, double y, double theta, const std::vector<geometry_msgs::Point>& footprint_spec, double inscribed_radius = 0.0, double circumscribed_radius=0.0){
double cos_th = cos(theta);
double sin_th = sin(theta);
std::vector<geometry_msgs::Point> oriented_footprint;
for(unsigned int i = 0; i < footprint_spec.size(); ++i){
geometry_msgs::Point new_pt;
new_pt.x = x + (footprint_spec[i].x * cos_th - footprint_spec[i].y * sin_th);
new_pt.y = y + (footprint_spec[i].x * sin_th + footprint_spec[i].y * cos_th);
oriented_footprint.push_back(new_pt);
}
geometry_msgs::Point robot_position;
robot_position.x = x;
robot_position.y = y;
if(inscribed_radius==0.0){
costmap_2d::calculateMinAndMaxDistances(footprint_spec, inscribed_radius, circumscribed_radius);
}
return footprintCost(robot_position, oriented_footprint, inscribed_radius, circumscribed_radius); //返回纯虚函数
}
virtual ~WorldModel(){}
protected:
WorldModel(){}
};
};
派生类函数,及纯虚函数实现定义:
costmap_model.cpp
#include <base_local_planner/world_model.h>
#include <costmap_2d/costmap_2d.h>
namespace base_local_planner {
// CostmapModel为WorldModel的派生类
class CostmapModel : public WorldModel {
public:
CostmapModel(const costmap_2d::Costmap2D& costmap);
virtual ~CostmapModel(){}
using WorldModel::footprintCost;
virtual double footprintCost(const geometry_msgs::Point& position, const std::vector<geometry_msgs::Point>& footprint,
double inscribed_radius, double circumscribed_radius);
};
// 纯虚函数实现
double CostmapModel::footprintCost(const geometry_msgs::Point& position, const std::vector<geometry_msgs::Point>& footprint,
double inscribed_radius, double circumscribed_radius){
unsigned int cell_x, cell_y;
//now we really have to lay down the footprint in the costmap grid
unsigned int x0, x1, y0, y1;
double line_cost = 0.0;
double footprint_cost = 0.0;
//we need to rasterize each line in the footprint
for(unsigned int i = 0; i < footprint.size() - 1; ++i){
//get the cell coord of the first point
if(!costmap_.worldToMap(footprint[i].x, footprint[i].y, x0, y0))
return -3.0;
//get the cell coord of the second point
if(!costmap_.worldToMap(footprint[i + 1].x, footprint[i + 1].y, x1, y1))
return -3.0;
line_cost = lineCost(x0, x1, y0, y1);
footprint_cost = std::max(line_cost, footprint_cost);
//if there is an obstacle that hits the line... we know that we can return false right away
if(line_cost < 0)
return line_cost;
}
line_cost = lineCost(x0, x1, y0, y1);
footprint_cost = std::max(line_cost, footprint_cost);
if(line_cost < 0)
return line_cost;
//if all line costs are legal... then we can return that the footprint is legal
return footprint_cost;
}
};
调用虚函数及纯虚函数定义:
obstacle_cost_function.cpp
#include <base_local_planner/trajectory_cost_function.h>
#include <base_local_planner/costmap_model.h>
#include <costmap_2d/costmap_2d.h>
namespace base_local_planner {
class ObstacleCostFunction : public TrajectoryCostFunction {
public:
ObstacleCostFunction(costmap_2d::Costmap2D* costmap);
~ObstacleCostFunction();
static double footprintCost(
const double& x,
const double& y,
const double& th,
double scale,
std::vector<geometry_msgs::Point> footprint_spec,
costmap_2d::Costmap2D* costmap,
base_local_planner::WorldModel* world_model);
private:
costmap_2d::Costmap2D* costmap_;
base_local_planner::WorldModel* world_model_;
};
ObstacleCostFunction::ObstacleCostFunction(costmap_2d::Costmap2D* costmap)
: costmap_(costmap), sum_scores_(false) {
if (costmap != NULL) {
world_model_ = new base_local_planner::CostmapModel(*costmap_);
}
}
double ObstacleCostFunction::footprintCost (
const double& x,
const double& y,
const double& th,
double scale,
std::vector<geometry_msgs::Point> footprint_spec,
costmap_2d::Costmap2D* costmap,
base_local_planner::WorldModel* world_model) {
double footprint_cost = world_model->footprintCost(x, y, th, footprint_spec);
if (footprint_cost < 0) {
return -6.0;
}
unsigned int cell_x, cell_y;
double occ_cost = std::max(std::max(0.0, footprint_cost), double(costmap->getCost(cell_x, cell_y)));
return occ_cost;
}
}
实例分析
分析一下整个函数调用和实现的过程:首先在ObstacleCostFunction
类中函数中
footprintCost(const double& x,
const double& y,
const double& th,
double scale,
std::vector<geometry_msgs::Point>footprint_spec,
costmap_2d::Costmap2D* costmap,
base_local_planner::WorldModel* world_model)
计算footprint_cost
时,使用world_model
基类指针,它指向派生类CostmapModel
,此处定义在上述调用函数中已经声明了,如下:
base_local_planner::WorldModel* world_model_;//在调用函数中声明
world_model_ = new base_local_planner::CostmapModel(*costmap_);//在调用函数中声明了指向派生类
此时,world_model->footprintCost(x, y, th, footprint_spec)
调用的是基类的普通函数double footprintCost(double x, double y, double theta, const std::vector<geometry_msgs::Point>& footprint_spec, double inscribed_radius = 0.0, double circumscribed_radius=0.0)
,因为派生类中没有函数覆盖它。在此普通函数返回值调用了return footprintCost(robot_position, oriented_footprint, inscribed_radius, circumscribed_radius)
返回的是纯虚函数,而此纯虚函数在派生类中实现。
虚析构函数
可以看到在基类析构函数声明为虚析构函数virtual ~WorldModel(){}
,这样做的目的是:基类的析构函数声明为虚函数后,派生类的析构函数也会自动成为虚函数。这个时候编译器会忽略指针的类型,而根据指针的指向来选择函数;也就是说,指针指向哪个类的对象就调用哪个类的函数。当指针指向派生类的对象时,会调用派生类的析构函数,继而再调用基类的析构函数。这样就避免了基函数只调用了基类的析构函数,没有调用派生类的析构函数而产生的内存泄露的问题。