我已经尝试了很多方法来解决这个问题,但都没有奏效。我想要实现的是这个。
我有这样一个类。
class Auton {
public:
int slot;
const char * name;
void run();
};
我想做的是让 run
函数做不同的事情。例如-
// The code below doesn't work, unfortunately :(
Auton one;
void one::run() {
// Do something here
}
Auton two;
void two::run() {
// Do something different here
}
这可能吗?
最佳答案
可以用 Lambda expressions .
示例:
class Auton {
public:
int slot;
const char * name;
std::function<void()> run;
};
Auton one;
one.run = [] {
// Do something here
};
Auton two;
two.run = [] {
// Do something different here
};
关于c++ - 你能声明一个成员函数并让它们做不同的事情吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56729873/