问题描述
大家好...
最近我尝试访问同一类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?
推荐答案
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();
}
class CMyWnd : CDialog {
它应该是
and it should probably be
class CMyWnd : public CDialog {
这篇关于lamda表达式中访问成员函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!