步骤1:定义一个函数指针!
class Thing {
private:
int val;
// void func(); declares a function that will be defined later
// not what we want.
using func_ptr = void (*)(); // define a new type. It make everything
// that follows much easier
func_ptr func; // declares func to be a pointer to a function
public:
Thing(int val = 0): val(val) // use member initializer list
// all members must be initialized
// before entering the body of the
// constructor. not that important
// for an int, but very important for
// types with expensive initialization
// or no default initialization
{
}
void setFunc(func_ptr func) // pass a function pointer
{
this->func = func; // Do not call the function!
// avoid reusing names in the same scope.
// It only leads to trouble
}
void useFunc()
{
func(); // which func is not ambiguous here. No need for this
}
};
步骤2:分配函数!
thing1.setFunc({
std::cout << "i am thing 1, my value is: " << this->val;
});
看起来你正试图使用
Lambda Expression
。这里有几个问题。
-
语法错误。C++不是一种值得猜测的语言。
Get a good book or two
并以可控的方式学习语法。另一种选择是缓慢、痛苦和不值得做。
正确的语法看起来像
thing1.setFunc([](){
std::cout << "i am thing 1, my value is: " << this->val;
});
-
不幸的是,Lambda不知道它与
thing1
它也不是
Thing
所以它不能使用
this
。这有点让人欲罢不能。Lambda需要
俘虏
事物1
,但是一旦捕获,就不能再使用函数指针了。它们太简单了,无法处理捕获。进来
std::function
。
3.从
std::函数
#include <iostream>
#include <functional>
class Thing {
private:
int val;
using func_ptr = std::function<void ()>;
func_ptr func;
public:
Thing(int val = 0): val(val)
{
}
void setFunc(func_ptr func)
{
this->func = func;
}
void useFunc()
{
func();
}
int get_val()
{
return val;
}
};
int main() {
Thing thing1 = Thing(1);
thing1.setFunc([thing1](){ // captures by value. Not always what you
// want, but generally the safest option.
// Just fine for a simple example like this
std::cout << "i am thing 1, my value is: " << thing1.val;
});
thing1.useFunc();
return 0;
}
但这仍然不起作用,因为
Thing::val
是私人成员,无法从外部访问
事情
或的朋友
事情
Lambda两者都不是,也不容易被塑造成一体。A)Lambda必须调用
事情
成员函数来做真正的工作,毫无意义,因为你会放弃所有这些繁琐的工作,直接调用成员函数,B)你为
物品::val
Lambda使用你制造的吸气剂
物品::val
public
。
B看起来是最好的选择。但如果你需要的逻辑必须改变呢
事物1
?这需要一个。。。
第四步:找到一个更简单的答案来解决首要问题。