Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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:

C++
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:

C++
#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
Updated 9-Jan-13 21:44pm
v2

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:
C++
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:
C++
void ClassA::runme()
{
    std::function<void (void)> 
        func0=[&](void) { ::callme(); },
        func1=[&](void) { callme(); };
    func0();
    func1();
}
 
Share this answer
 
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 {
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900