AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 校园
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

ROS学习第8天

作者: 作者的头像   jinyc ,  2022-06-22 15:34:43 ,  所有人可见 ,  阅读 24


0


回调过程中被调用的函数被称作回调函数,调用的函数叫主调函数

怎么使用带参数的回调函数?

#include<stdio.h>

int Callback_1(int x) // Callback Function 1
{
    printf("Hello, this is Callback_1: x = %d ", x);
    return 0;
}

int Handle(int y, int (*Callback)(int))
{
    printf("Entering Handle Function. ");
    Callback(y);
    printf("Leaving Handle Function. ");
}

int main()
{
    int a = 2;
    printf("Entering Main Function. ");
    Handle(a, Callback_1);
    printf("Leaving Main Function. ");
    return 0;
}

运行结果
Entering Main Function.
Entering Handle Function.
Hello, this is Callback_1: x = 2
Leaving Handle Function.

this指针的一个示例

#include <iostream>

using namespace std;

class Box
{
   public:
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      int compare(Box box)
      {
         return this->Volume() > box.Volume();
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};

int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2

   if(Box1.compare(Box2))
   {
      cout << "Box2 is smaller than Box1" <<endl;
   }
   else
   {
      cout << "Box2 is equal to or larger than Box1" <<endl;
   }
   return 0;
}

输出结果:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1

cmd_sub_ = nh_.subscribe<geometry_msgs::Twist>("cmd_vel",10,&XtarkDriver::cmd_vel_callback,this);
//this 指的是Twist

使用boost.bind绑定class的成员函数作为boost thread的入口


class HelloWorld
{
public:
    void hello();
    void entry();
};

void HelloWorld::hello() 
{ 
    std::cout << "Hello world, I'm a thread!" << std::endl;
}

int main(int argc, char* argv[]) 
{ 
    HelloWorld hw;
    hw.entry();
    return 0;
}
void HelloWorld::entry()
{
    boost::thread thrd(boost::bind(&HelloWorld::hello,this));
    thrd.join();
}

this指针是 T * const 类型,表示不能重定向它的指向,顶层const 指针常量(指针是一个常量)

int * const p = &a;

底层const 常量指针(指向常量的指针)

int const * p = &a;  等价于 const int * p = &a;

转载自 https://blog.csdn.net/fangfanglovezhou/article/details/124553770

0 评论

你确定删除吗?

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息