Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Lambda
Hi all...
 
Recently I try to access another member function in the same class of lambda expression. (VS2010)
 
The skeleton of my program looks like this:
 
class CMyWnd : CDialog {
	...
	void OnMsg(void) {
		...
		GetDlgItem(IDC_CTRL)->EnableWindow(FALSE);
		...
	}
};
 
Then I got 2 complains from compiler:
error C2660: 'GetDlgItem' : function does not take 1 arguments
and
error C2227: left of '->EnableWindow' must point to class/struct/union/generic type
 
It looks like compiler failed to determine the scope of function.
(CWnd::GetDlgItem and ::GetDlgItem)
If I insert this-> in front of GetDlgItem, compilation finishes successfully.
 
To validation this problem, I wrote a little test program like this:
 
#include <cstdio>
#include <functional>

void callme(void) {
	printf("::callme!\n");
}
 
class ClassA {
	protected:
 
	void callme(void) {
		printf("ClassA::callme!\n");
	};
	
	public:
	
	void runme(void) {
		std::function<void (void)> func0=[&](void) {
			callme();
		},
		func1=[&](void) {
			this->callme();
		};
		
		func0();
		func1();
	}
};
 
int main(int argc, char *argv[]) {
	ClassA a;
	
	a.runme();
	
	return 0;
}
 
Under VS2010, the result is:

::callme!
ClassA::callme!

 
It disappointed me.
 
Can anyone share better workarounds or results of another compiler?
Posted 9-Jan-13 21:41pm
Edited 9-Jan-13 21:44pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

When using similar names in different scopes, at least one must be prefixed. Because you are implementing code inside class declarations, you must use this here. Imagine that the code is inline and the code block is copied as-is to the location where the function is called before compilation (replacing only the this pointer to the class variable name). The result would look like this:
int main(int argc, char *argv[]) {
    ClassA a;
 
//  inline expansion of a.runme():
    {
        std::function<void (void)> 
            func0=[&](void) { callme(); },
            func1=[&](void) { a.callme(); };
        func0();
        func1();
    }
    return 0;
}
If you would define the runme() function outside the class declaration, the prefix for the global scope callme() function must be used:
void ClassA::runme()
{
    std::function<void (void)> 
        func0=[&](void) { ::callme(); },
        func1=[&](void) { callme(); };
    func0();
    func1();
}
 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You might be running into problems with access. The first line of your class is
class CMyWnd : CDialog {
and it should probably be
class CMyWnd : public CDialog {
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141002.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100