首页 > 解决方案 > 如何将对象函数的实例传递给另一个函数?

问题描述

我有一个类试图封装中断的设置。我需要将实例化的引用/指针/等传递给外部函数,然后调用我的函数。

我无法更改此功能:

typedef void (*voidFuncPtr)(void);
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode);

我的图书馆:


class Buttons ... 

bool Buttons::begin(int buttonPin)
{
    //attachInterrupt(buttonPin, (this->*func)Buttons::released, HIGH);
    attachInterrupt(buttonPin, &Buttons::released, LOW);

return 0;
}

void Buttons::released()
{
    numButtonPresses++;
    pressLength = millis()-pressStartTime;
    pressStartTime = 0;
    buttonState=LOW;
}

问题是我不知道在 Buttons::begin 方法的 attachInterupt 函数中放什么。我确信我在处理这个问题的方式上做错了,但我不确定它是什么或我应该怎么做。我将不胜感激任何建议!

标签: c++

解决方案


您正在使用旧的 c 样式函数指针回调。这不适用于对象的成员函数。如果无法更改回调,则需要将 Buttons::released 函数设为静态。


推荐阅读