0
이것이 가능할 지 모르겠습니다. 내 함수 중 하나가 함수 객체를 c 라이브러리 함수에 전달해야하고 함수 포인터 만 사용할 수 있기 때문에. C 함수 포인터를 C 함수 포인터로 바꿈
나는 데모 프로그램을 만든 나는 내 목적을 설명하기 위해 충분하다 생각 :#include <iostream>
#include <functional>
class Test;
void init_class(std::function<int (int)> fn) {
int (*new_fn)(int) = nullptr; // I can't assign fn to new_fn either <<<<<<<<<<<<
new_fn = fn; // this complains <<<<<<<<<<<<<<<<<<<<<<<<<
std::cout << new_fn(199) << std::endl;
}
class Test {
public:
explicit Test()
: n_(199) {
}
~Test() noexcept {}
int calculate(int val) {
return n_ + val;
}
void run() {
std::function<int (int)> fn =
std::bind(&Test::calculate, this, std::placeholders::_1);
init_class(fn);
}
private:
int n_;
};
void test() {
Test a;
a.run();
}
int main(int argc, const char *argv[]) {
test();
return 0;
}
그렇게 할 수 없습니다. 당신이 원하는 어떤 상태에 대해서도 전역을 읽는 평범한 함수를 가지고 있다는 끔찍한 해킹을 사용할 수는 있지만 그게 전부입니다. – Jon