gpt4 book ai didi

c++ - 带/不带类的回调函数指针 C++

转载 作者:IT老高 更新时间:2023-10-28 23:17:08 28 4
gpt4 key购买 nike

我卡住了。我正在尝试形成一个函数,它将吃掉无类函数指针和对象中的指针。这是我当前的代码,希望能解释更多。

(它应该在 Arduino 上运行,所以我不能使用大型库。)

首先,我将这个库用于 Arduino:

/* SimpleTimer - A timer library for Arduino.
* Author: mromani@ottotecnica.com
* Copyright (c) 2010 OTTOTECNICA Italy
*/

它采用在这种类型的设置计时器间隔上调用的函数:

typedef void (*timer_callback)(void);

据我所知,这是一个类函数,网页 Pointers to member functions 让我走得很远,但还不够远。可能是我的术语不足。

现在,我创建了自己的类,我想反过来使用这个 SimpleTimer 库。但是,如果我将 SimpleTimer 提供给我的类函数,它就不喜欢它们(据我所知)。但是如何在不改变 SimpleTimer 库的情况下实现这一点。

所以就有了 Robot 类,它有 Robot::halt()。我希望机器人在一定时间内向前移动。像这样:

void Robot::forward(int speed, long time) {
reset();
timer.setTimer(time, c_func, 1);

analogWrite(l_a, speed);
analogWrite(r_a, speed);
isMoving(true);
}

void Robot::halt() {
__isMoving = false;
digitalWrite(r_a, LOW);
digitalWrite(r_b, LOW);
digitalWrite(l_b, LOW);
digitalWrite(l_a, LOW);
}

c_func 变量此时是一个无类函数,但我想使用 Robot::halt 函数。我看过,读过,学过,但还没有成功。我似乎无法理解这个,因为我错过了一些角度。

我试过了:

timer.setTimer(time, (this->*halt), 1);
timer.setTimer(time, Robot::*halt, 1);
timer.setTimer(time, &Robot::halt), 1);

但是这都是同一个问题/我只是在这里暗中刺伤......

编辑

之前,我说过不想更改 SimpleTimer 库代码。我想回归这个,我想改变它会有更好的选择。

已经感谢所有当前的答案,我只被允许将一个标记为可行的答案,实际上我在这里阅读的所有内容都非常有帮助。

要继续此操作,请更改 SimpleTimer 代码。这个类需要引用包含我的“暂停”函数的对象,对吧?因此,将 settimer 函数重载为将我的对象和我的函数作为两个单独的指针的东西会起作用......?我想我已经掌握了这个窍门,但是,我还没有头脑清醒。

编辑

我不知道谁又来了这个,但是,任何人都找到了这个线程。如果找到 Member Function Pointers and the Fastest Possible C++ Delegates 对函数指针和成员函数指针进行了很好的介绍。

编辑

得到它的工作,更改 SimpleTimer 库以使用此委托(delegate)系统: http://www.codeproject.com/KB/cpp/FastDelegate.aspx

它集成得非常好,在 Arduino 库中拥有这样的标准 Delegate 系统会很不错。

测试中的代码(工作)

类型定义

typedef FastDelegate0<> FuncDelegate;

机器人类中的代码:

void Robot::test(){
FuncDelegate f_delegate;
f_delegate = MakeDelegate(this, &Robot::halt);

timer.setTimerDelg(1, f_delegate, 1);
}

void Robot::halt() {
Serial.println("TEST");
}

SimpleTimer 类中的代码:

int SimpleTimer::setTimerDelg(long d, FuncDelegate f, int n){
f();
}

Arduino 在控制台中打印 TEST。

下一步将它放入一个数组中,在那里看不到很多问题。谢谢大家,这两天学到的东西我简直不敢相信。

那是什么味道?那是……的味道吗?成功!

对于感兴趣的人来说,使用的Delegate系统并不构成内存容量问题:使用 FastDelegate

AVR Memory Usage
----------------
Device: atmega2560

Program: 17178 bytes (6.6% Full)
(.text + .data + .bootloader)

Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)


Finished building: sizedummy

没有 FastDelegate:

AVR Memory Usage
----------------
Device: atmega2560

Program: 17030 bytes (6.5% Full)
(.text + .data + .bootloader)

Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)


Finished building: sizedummy

最佳答案

您可以通过创建 functor 来做到这一点对象,它充当计时器代码和您的代码之间的代理。

class MyHaltStruct
{
public:
MyHaltStruct(Robot &robot)
: m_robot(robot)
{ }

void operator()()
{ robot.halt(); }

private:
Robot &m_robot;
}

// ...

timer.setTimer(time, MyHaltStruct(*this), 1);

编辑

如果不能通过仿函数对象完成,您可以改为全局变量和函数,也许在命名空间中:

namespace my_robot_halter
{
Robot *robot = 0;

void halt()
{
if (robot)
robot->halt();
}
}

// ...

my_robot_halter::robot = this;
timer.setTimer(time, my_robot_halter::halt, 1);

这仅在您有一个机器人实例时才有效。

关于c++ - 带/不带类的回调函数指针 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com