Why is "this" enclosed with square brackets here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is "this" enclosed with square brackets here?

#include <Wt/WApplication.h> #include <Wt/WBreak.h> #include <Wt/WContainerWidget.h> #include <Wt/WLineEdit.h> #include <Wt/WPushButton.h> #include <Wt/WText.h> class HelloApplication : public Wt::WApplication { public: HelloApplication(const Wt::WEnvironment& env); private: Wt::WLineEdit *nameEdit_; Wt::WText *greeting_; }; HelloApplication::HelloApplication(const Wt::WEnvironment& env) : Wt::WApplication(env) { setTitle("Hello world"); root()->addWidget(std::make_unique<Wt::WText>("Your name, please? ")); nameEdit_ = root()->addWidget(std::make_unique<Wt::WLineEdit>()); Wt::WPushButton *button = root()->addWidget(std::make_unique<Wt::WPushButton>("Greet me.")); root()->addWidget(std::make_unique<Wt::WBreak>()); greeting_ = root()->addWidget(std::make_unique<Wt::WText>()); auto greet = [this]{ greeting_->setText("Hello there, " + nameEdit_->text()); }; button->clicked().connect(greet); } int main(int argc, char **argv) { return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) { return std::make_unique<HelloApplication>(env); }); }

25th Nov 2018, 2:34 PM
Pedro Victor Brito Cordeiro
Pedro Victor Brito Cordeiro - avatar
2 Answers
+ 5
I made a query on the Webtoolkit forum's search bar and there was nothing in particular except some discussion about `connect() and lambda`. https://redmine.webtoolkit.eu/projects/wt/search?utf8=%E2%9C%93&q=%5Bthis%5D It's much better to ask your question there, too. https://redmine.webtoolkit.eu/projects/wt/boards/2 _____ https://en.cppreference.com/w/cpp/language/this https://en.cppreference.com/w/cpp/language/lambda
25th Nov 2018, 3:08 PM
Babak
Babak - avatar
+ 1
Oh, it seems to be the C++'s way of doing anonymous function callbacks. Thanks for the links :)
25th Nov 2018, 6:47 PM
Pedro Victor Brito Cordeiro
Pedro Victor Brito Cordeiro - avatar