I am training to encapsulate some codes and for this I created three classes, which are Vero, Pionner and Vehicle. The first two classes have the member variable cmd and the member function getcmd - in each classes theses members are slightly different. Then. we have the class Vehicle, which is a subclass of Vero and Pionner. The Vehicle constructor has the parameter which_car that should be used to select which of the two classes we want to work with.
I would like to have some kind of function pointer that could point to the getcmd member function of the class Vero or of the class Pionner. How could a perform this task?
COMMENT: I am not asking about how to just create a member point. I am asking how can I, somehow, create a generic type that can point or to Vero objects or to Pionner objects.
The code I am working on.
HEADER
#ifndef VEHICLE_H
#define VEHICLE_H
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <ransac_project/CarCommand.h>
class Vero{
public:
ransac_project::CarCommand cmd;
Vero();
void getcmd(const double &velocity, const double &steering);
};
class Pionner{
public:
geometry_msgs::Twist cmd;
Pionner();
void getcmd(const double &linear_vel, const double &angular_vel);
};
class Vehicle: public Vero, public Pionner{
public:
Vehicle(const std::string &which_car);
void (*arrangemessage) (const double &, const double &);
};
#endif /* VEHICLE_H */
SOURCE
#include "vehicle.hpp"
Vero::Vero(){
cmd.speedLeft = 0.0; cmd.speedRight = 0.0; cmd.steerAngle = 0.0;
return;
}
void Vero::getcmd(const double &velocity, const double &steering){
cmd.speedLeft = velocity;
cmd.speedRight = velocity;
cmd.steerAngle = steering;
return;
}
Pionner::Pionner(){
cmd.linear.x = 0.0; cmd.linear.y = 0.0; cmd.linear.z = 0.0;
cmd.angular.x = 0.0; cmd.angular.y = 0.0; cmd.angular.z = 0.0;
return;
}
void Pionner::getcmd(const double &linear_vel, const double &angular_vel){
cmd.linear.x = linear_vel;
cmd.linear.y = 0;
cmd.linear.z = 0;
cmd.angular.x = 0;
cmd.angular.y = 0;
cmd.angular.z = angular_vel;
return;
}
Vehicle::Vehicle(const std::string &which_car){
if(which_car.compare("vero")==0){
arrangemessage = &Vero::getcmd;
}
else if(which_car.compare("pionner")==0){
arrangemessage = &pionner.getcmd;
}
else{
ROS_INFO_STREAM("which_car must be vero or pionner");
exit(0);
}
}