c++ - 如何将对象函数的实例传递给另一个函数?
问题描述
我有一个类试图封装中断的设置。我需要将实例化的引用/指针/等传递给外部函数,然后调用我的函数。
我无法更改此功能:
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 样式函数指针回调。这不适用于对象的成员函数。如果无法更改回调,则需要将 Buttons::released 函数设为静态。
推荐阅读
- python - 在 VSCode 中运行脚本时找不到 bson 模块,在终端的相同 conda 环境中工作正常
- html - 烧瓶 | HTML 不呈现通过 render_template 发送的数据
- google-cloud-firestore - Firestore add 为两个文档生成相同的文档 ID?
- r - 如何将多元丰度矩阵转换为R中的出现表?
- html - 位置固定在子元素中,但同步父 x 滚动与子元素左侧位置
- apache-spark - Spark Dataframe 默认是如何分区的?
- android - 如何从 ViewPager 片段中打开另一个片段?
- node.js - Gulp 项目在本地运行但无法构建
- asynchronous - 错误 TIMEOUT WAITING FOR ACK - RabbitMq Publisher 确认异步实现
- mongodb - MongoDB聚合从包含变量的路径中获取值