我在多态类中具有以下成员,Parent并且我正在寻找一种替换foo为某种虚拟成员的方法bar:
void Parent::myFunc() {
// lots of variables
// complicated calculations
while (/* loop condition */) {
// more variables and calculations
auto foo = [&](int n) {/* ... */};
foo(42);
// want to replace with virtual
// bar(42);
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是foo捕获了所有内容,而且我不知道授予bar相同访问权限的正确方法。
baras参数会导致参数列表很大,这似乎不是一个很好的解决方案myFunc()转换成的成员Parent,但这会不必要地延长这些变量的寿命,尤其是对于循环变量。