我是一名中级C++用户,遇到以下情况。下面显示的类定义可以用g ++编译器编译。但我不能把手指放在整个语法的意义上。
我的猜测是功能operator int()
返回一个int
类型。隐式转换运算符重载语法
而且,我无法弄清楚如何在main()
class A
{
public:
A(int n) { _num = n; } //constructor
operator int();
private:
int _num;
};
A::operator int() // Is this equivalent to "int A::operator()" ??
{
return _num;
}
int main()
{
int x = 10;
A objA(x); //creating & initializing
// how to use operator() ?
// int ret = objA(); // compiler error when uncommented
return 0;
}
任何帮助将不胜感激使用重载operator()
。
重载'运算符()'?什么重载'operator()'?你的代码没有任何重载的'operator()'。这就是为什么你不能使用它。 – AnT 2010-09-03 06:53:48
是的。我非常误会关键字操作符把我带到其他地方。 – vthulhu 2010-09-03 07:15:12
[此C++语法的含义是什么以及它为什么可以工作?]的可能的重复(http://stackoverflow.com/questions/3632746/what-does-this-c-syntax-mean-and-why-does-it工作) – sbi 2010-09-03 07:47:22