c++ lambda表达式捕获类数据成员 this
(2014-06-07 16:49:02)
标签:
itclambdac11捕获 |
分类: C/Cpp |
using namespace std;
class Kitty {
public:
private:
};
int main() {
}
If you gave me 0 toys, I would have 5 toys total.
If you gave me 1 toys, I would have 6 toys total.
If you gave me 2 toys, I would have 7 toys total.
当你捕获了this以后,m_toys就可以使用了,它隐式的表示this->m_toys,你也可以显示的说明this->m_toys。(在lambda表达式中,只有捕获了this后才可以使用它,你永远无法得到lambda表达式本身的this指针)
你也可以隐式的捕获this:
using namespace std;
class Kitty {
public:
private:
};
int main() {
}
C:\Temp>cl /EHsc /nologo /W4 implicitmemberkitty.cpp > NUL
&& implicitmemberkitty
If you gave me 0 toys, I would have 5 toys total.
If you gave me 1 toys, I would have 6 toys total.
If you gave me 2 toys, I would have 7 toys total.
你也可以使用“[&]”,但是它不会影响this的捕获方式(永远按值传递)。“[&this]”是不允许的。