我遇到問題。我試圖將一個void *轉換爲std :: function。 這只是一個簡單的例子,有什麼建議可以理解將void *轉換爲std :: function
#.h file
class Example {
public:
Example();
int foo(void* hi);
int fooFunc(std::function<int(int, int)> const& arg, int x, int y) {
foo(arg.target<void*>(), x, y);
return 2;
}
};
#.cpp file
Example::Example() {
}
int Example::foo(void * func, int x, int y)
{
//cast back to std::function
func(x, y);
std::cout << "running in foo: " << a << "\n";
return a;
}
每個鑄件我試過沒有工作。
我知道我可以在這個例子中發送一個std::function
,但它是爲了更大的東西,我正在做一個例子使它在這裏工作。
void*
的全部含義是有時候會使用它,在這些情況下,當你不知道你會收到什麼,然後將其轉換爲你需要的具體用法。
謝謝!
你爲什麼這樣做?什麼是來自(某些'dlsym'?)的'void *'。請注意,閉包比一個指針大。 –
一些dsystem庫。如果我不需要。我不會這樣做 – Mumfordwiz
不可能在'std :: function'中存儲'void *',因爲它不可調用。您對'target'的調用將始終返回一個空指針。 –