今天看到了一份代码
class PX4FLOW: public device::I2C, public I2CSPIDriver<PX4FLOW>
{
public:
PX4FLOW(I2CSPIBusOption bus_option, int bus, int address, uint8_t sonar_rotation, int bus_frequency,
int conversion_interval = PX4FLOW_CONVERSION_INTERVAL_DEFAULT, enum Rotation rotation = ROTATION_NONE);
virtual ~PX4FLOW();
...
...
...
PX4FLOW::PX4FLOW(I2CSPIBusOption bus_option, int bus, int address, uint8_t sonar_rotation, int bus_frequency,
int conversion_interval, enum Rotation rotation) :
I2C(DRV_FLOW_DEVTYPE_PX4FLOW, MODULE_NAME, bus, address, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus, address),
_sonar_rotation(sonar_rotation),
_sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")),
_comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": com_err")),
_sensor_rotation(rotation)
{
}
PX4FLOW::~PX4FLOW()
{
perf_free(_sample_perf);
perf_free(_comms_errors);
}
...
...
...
int
px4flow_main(int argc, char *argv[])
{
int ch;
using ThisDriver = PX4FLOW;
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
}
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
}
很是不理解
仔细去看了一下关于C++的一些东西,就是构造函数与虚函数。
关于构造函数
看菜鸟上讲得十分到位
添加链接描述
类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。
构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。
构造函数也可以认为是一个普通的函数来看,欧,不对这里应该叫做方法
在一个类里面是没有函数这个概念才是正确的想法,它只是声明了一些属性和方法(私有或者公有),然后在类外(或者类里)定义函数来实现这个方法,对,我就是这样的理解。
好的这个构造函数是不可以返回东西的,所以关于返回的我们不管
等等,不是说不返回东西吗?为什么会出现ThisDriver = PX4FLOW;
这种,好的,这里跟构造函数没关系,这里跟构造函数没得关系,这里是实例化了一个对象。。。具体要去看C++实例化对象的方式和using这个关键词的说法。
麻烦的就是带参数的
带参数的定义有很多不同的方式
以菜鸟的举例
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line( double len)
{
cout << "Object is being created" << endl;
}
...
...
...
// 程序的主函数
int main( )
{
Line line(10.0);
// 获取默认设置的长度
cout << "Length of line : " << line.getLength() <<endl;
// 再次设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
最常见的用法是在实例化这个对象时带上这个参数就像上面样
当然这个参数也可以有一个默认值类似与这样子的操作
#include <iostream>
using namespace std;
class Box{
public:
Box(int h=2,int w=2,int l=2);//在声明构造函数时指定默认参数
int volume();
private:
int height,width,length;
};
Box::Box(int h,int w,int len){//在定义函数时可以不指定默认参数
height=h;
width=w;
length=len;
}
int Box::volume(){
return height*width*length;
}
int main(){
Box box1(1);//不指定第2、3个实参
cout<<"box1's volume: "<<box1.volume()<<endl;
Box box2(1,3);// 不指定第3个实参
cout<<"box2's volume: "<<box2.volume()<<endl;
Box box3;
cout<<"box3's volume:"<<box3.volume()<<endl;
return 0;
}
很明显上面这些不是我要看到的
所以就有看到了使用初始化列表来初始化字段这种操作
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // 这是构造函数
private:
double length;
};
Line::Line( double len): length(len)
{
cout << "Object is being created, length = " << len << endl;
}
所以我看到的代码实际上是通过使用初始化列表来初始化字段来做了一个构造函数
析构函数
关于这个操作我已将学到了
PX4FLOW::PX4FLOW(I2CSPIBusOption bus_option, int bus, int address, uint8_t sonar_rotation, int bus_frequency,
int conversion_interval, enum Rotation rotation) :
I2C(DRV_FLOW_DEVTYPE_PX4FLOW, MODULE_NAME, bus, address, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus, address),
_sonar_rotation(sonar_rotation),
_sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")),
_comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": com_err")),
_sensor_rotation(rotation)
{
}
但是下面一个操作又迷了我
PX4FLOW::~PX4FLOW()
{
perf_free(_sample_perf);
perf_free(_comms_errors);
}
好吧我C++确实有一点半吊子
virtual ~PX4FLOW();
这句话是定义了一个析构函数
类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。
好嘛,知道了,没什么特别的
这里用了一个virtual关键字,我估计是为了在其它类里面实例化这个类的时候来做析构函数。