lamda表达式中访问成员函数的问题 [英] A problem of access member function in lamda expression

查看:147
本文介绍了lamda表达式中访问成员函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...



最近我尝试访问同一类lambda表达式中的另一个成员函数。 (VS2010)



我程序的骨架如下:



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);
		...
	}
};





然后我从编译器得到2个抱怨:

错误C2660:''GetDlgItem'':函数不带1个参数



错误C2227:左边的'' - > EnableWindow ''必须指向class / struct / union / generic类型



看起来编译器无法确定函数的范围。

( CWnd :: GetDlgItem和:: GetDlgItem)

如果我插入这个 - >在GetDlgItem之前,编译成功完成。



为了验证这个问题,我写了一个像这样的小测试程序:





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;
}





在VS2010下,结果是:



:: callme!

ClassA :: callme!





It让我很失望。



任何人都可以分享更好的解决方法或其他编译器的结果吗?



Under VS2010, the result is:

::callme!
ClassA::callme!


It disappointed me.

Can anyone share better workarounds or results of another compiler?

推荐答案

当使用类似的时候在不同范围内的名称,至少有一个必须加前缀。因为您在类声明中实现代码,所以必须在此处使用 this 。想象一下,代码是内联的,代码块按原样复制到编译前调用函数的位置(只替换 this 指向类变量名的指针) 。结果如下所示:

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;
}



如果你要在类声明之外定义 runme()函数,前缀为必须使用全局范围 callme()函数:


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();
}


您可能遇到访问问题。你班上的第一行是

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 {


这篇关于lamda表达式中访问成员函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆