我想在程序中设置以下类:
首先,最小可重现的示例:
#include <vector>
#include <iostream>
#include <thread>
template <typename T>
class CallbackBuffer
{
public:
std::vector<T> buffer;
void (*callback)(std::vector<T>);
std::thread writerThread;
CallbackBuffer(int bufferSize = 10)
{
buffer.resize(bufferSize);
}
void setCallback(void (*cb)(std::vector<T>))
{
callback = cb;
}
void writeCall()
{
writerThread = std::thread(callback, buffer);
}
};
template <typename T>
class Base
{
public:
CallbackBuffer<T> buffer;
Base()
{
buffer.setCallback(bufferHandler);
}
void bufferHandler(std::vector<T> v)
{
for(auto &i : v)
{
write(i);
}
}
virtual void write(T i) = 0;
};
class Derived : public Base<int>
{
public:
Derived()
{
}
void write(int i)
{
std::cout << i << std::endl;
}
};
int main()
{
Derived d;
return 0;
}
我收到以下编译器错误:
error: invalid use of non-static member function ‘void Base<T>::bufferHandler(std::vector<T>) [with T = int]’
因此,编译器需要 bufferHandler 是静态的,但是如果我这样做了,那么我将无权访问该对象的成员。有没有办法解决这个问题,或者只是一个可怕的想法?
最佳答案
您正在传递类成员函数,因此需要在CallbackBuffer
类中添加以下内容:
void (Base<T>::*callback)(std::vector<T>);
// ...
void setCallback(void (Base<T>::*cb)(std::vector<T>)) {
callback = cb;
}
并在
Base
类中:Base() {
buffer.setCallback(&Base<T>::bufferHandler);
}
Demo
关于c++ - 可调用在C++类模板中必须是静态的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60973232/