此代码编译罚款:问题态类
int main()
{
class lm {
public:
int operator()(int x,int y){
return x+y;
}
};
int x = lm()(10,15);
return 0;
}
但这并不
int main()
{
template<class T>
class lm {
public:
T operator()(T x,T y){
return x+y;
}
};
int x = lm()(10,15);
return 0;
}
我为什么这样做呢? ,因为我想伪造另一个函数内部的多态lambda函数。 其他建议也欢迎。
最后我想这样做的是:
class A{
int m_var;
public:
int f(int x,int y);
}
int A::f(int x, int y)
{
template<class T>
class lm {
public:
T operator()(T x,T y){
return x+y+ m_var; //accessing the member variable of class A
}
};
int x = lm()(10,15);
return 0;
}
点是函数对象应该能够访问的class A
成员变量的任何变通办法也将有所帮助。