![](/img/trans.png)
[英]How can I pass a class method as a parameter to another function and later call it, preferably making the variable class method signature explicit?
[英]Explicit passing “this” parameter to method call
是否有可能在c ++調用類方法中顯式傳遞第一個“this”參數呢?
像這樣的東西:
struct A
{
void some() {}
};
....
A a;
A::some(&a); // ~ a.some();
對於合理的問題“為什么?”:我需要實現std :: bind analogue,它適用於這樣的結構:
void f(int);
bind(f, 3);
但這不起作用:
bind(&A::some, &a);
更新:大家好,我的問題顯然不是很清楚。 我知道如何使用std :: bind,我想知道如何處理這個param顯式傳遞給它的構造:std :: bind(&A :: some,&a);
以下是您可以在bind
使用的調度程序的想法:
template <class R, class... Arg>
R call(R (*f)(Arg...), Arg &&... arg)
{ return f(std::forward<Arg>(arg)...); }
template <class C, class R, class... Arg>
R call(R (C::*f)(Arg...), C &c, Arg &&... arg)
{ return (c.*f)(std::forward<Arg>(arg)...); }
你想要下面這樣的東西嗎?
struct A
{
void some();
static void some(A* that) { that->some(); }
};
..
A a;
A::some(&a);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.