在需要非成员函数指针时传递成员函数指针
Passing member function pointer when a non-member function pointer was expected?
>我目前在将成员函数回调函数传递给库portaudio定义的非成员函数时遇到了一些问题。
我的类是这样定义的:
class record {
public:
record();
void start_record();
int recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData);
private:
PaStreamParameters inputParameters,
outputParameters;
PaStream* stream;
PaError err = paNoError;
paTestData data;
int totalFrames;
int numSamples;
int numBytes;
SAMPLE max, val;
double average;
};
在start_record()
内,我将成员函数传递给非成员函数。这意味着我将类记录的成员函数(回调)传递给非成员函数 Pa_OpenStream()。
err = Pa_OpenStream(
&this->stream,
&this->inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
this->recordCallback,
&data );
if( err != paNoError )
{
std::cout << "Something wrong - open_stream check" << std::endl;
exit(1);
}
portaudio
需要类型的函数指针
int (*)(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*)
and not
而不是
int (record::)(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*)
一个快速的解决方案是在类范围之外定义函数,并使它在类中使用和定义的变量成为全局变量,但我想避免这种情况。那么我该如何处理呢?
您上一个问题的副本列出了一些想法,但缺乏具体的代码,所以这里有一个示例。
首先,通过添加静态回调函数来修改class record
:
static int myCallback(const void *inputBuffer, void outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData);
请注意,它与您现有的回调具有相同的签名,只是它是一个静态函数,因此您可以将其传递给 Pa_OpenStream
。
定义该函数以调用成员函数回调:
int record::myCallback(const void *inputBuffer, void outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
record *pThis = (record *)userData;
return pThis->recordCallback(inputBuffer, outputBuffer, framesPerBuffer, timeInfo, statusFlags, nullptr);
}
我们将nullptr
作为用户数据指针传递到此处,因为我们需要的所有用户数据都保存在类中。 由于此回调仅在内部使用,因此您可以从此处和函数中删除最后一个参数。
注册回调时,传入新回调,this
作为用户数据参数。
err = Pa_OpenStream(
&this->stream,
&this->inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
&this->myCallback,
this);
然后在recordCallback
,您可以像往常一样访问类的所有成员。
通常完成
此操作的方式是在数据上下文中传递"this"指针,稍后在回调中提取它。 可以将回调声明为简单的私有静态存根。
例:
class record
{
// ...
void start_record();
{
err = Pa_OpenStream(
&this->stream,
&this->inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
&record::recordCallbackStub, // call back our static stub
this ); // 'this' is the data
}
private:
// our member function called back, can be virtual
int recordCallback(
const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags) // no 'userData' param, since it's 'this'
{
// do your thing within the record context
}
// the stub to keep C happy, can be declared as extern "C" or whatever call
// convention is required from the library, WINAPI comes to mind.
static int recordCallbackStub(
const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags
void* userData)
{
auto pThis = reinterpret_cast<record*>(userData); // get back the this pointer.
return pThis->recordCallback( /* all parameters, except userData */);
}
};
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址