By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

C++0x. Lambda expressions: access to member functions. Is "this->" required?

P: n/a
The following code. Is "this->inc(n);" required or can it be replaced with "inc(n);"?

Expand|Select|Wrap|Line Numbers
  1. struct S10 
  2. {
  3.     int f;
  4.     double p;
  5.         void inc(int& a) { a += f;}
  6.     void work(int n) 
  7.     {
  8.         int m = n*n;
  9.         int j = 40;
  10.         f = 10;
  11.         p =3.7;
  12.         auto m3 = [this,&n]() mutable -> int
  13.         {
  14.             p++;
  15.             f++;       
  16.             this->inc(n); // inc(n);                
  17.             return n;
  18.         };
  19.         cout << "S10 m3():" << m3() << endl;
  20.         cout << "f:" << f << " p:" << p << " n:" << n << endl;
  21.     }
  22. };
  23.  
Oct 16 '10 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I have found the answer myself, asked the GCC team. The "this->inc(n);" can be replaced with "inc(n);". Since "this" is in the capture list, all the member objects are visible: the "inc" functions can be called by using its identifiers, the "this->" prefix is not required.
Oct 18 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.