- 论坛徽章:
- 0
|
10可用积分
#include <iostream>
using std::cout;
using std::endl;
class Point;
class Complex {
public:
Complex(int aReal = 0, int aImaginary = 0):iReal(aReal), iImaginary(aImaginary) {}
int Real() const { return iReal; }
int Imaginary() const { return iImaginary; }
void setReal(int aReal) { iReal = aReal; }
void setImagin(int aImaginary) { iImaginary = aImaginary; }
operator const Point&() const
{ cout << "operator const Point&() const" << endl; return *this; }
const Point& operator()() const
{ cout << "const Point& operator()() const" << endl; return *this; }
void Print() const { cout << iReal << " + " << iImaginary << "i" << endl; }
private:
int iReal;
int iImaginary;
};
class Point {
public:
Point(int aX = 0, int aY = 0):iX(aX), iY(aY) {}
Point(Point& aPoint):iX(aPoint.X()), iY(aPoint.Y()) {}
int X() const { return iX; }
int Y() const { return iY; }
void setX(int aX) { iX = aX; }
void setY(int aY) { iY = aY; }
void Print() const { cout << "(" << iX << ", " << iY << " " << endl; }
private:
int iX;
int iY;
};
void PrintPoint(const Point& aPoint)
{
cout << "3rd part print begin:" << endl;
aPoint.Print();
cout << "3rd part print end:" << endl;
}
int main(void)
{
Complex a(3, 2);
a.Print();
a().Print();
PrintPoint(a);
return 0;
}
|
为什么没有调用const Point& operator()() const, 而在调用operator const Point&() const的地方发生了死循环, 10分悬赏
[ 本帖最后由 accelerator 于 2009-3-12 10:11 编辑 ] |
最佳答案
查看完整内容
晕菜,你写的什么。。。仔细看看你自个儿的代码:a()首先调用const Point& operator()() const,而在const Point& operator()() const这里面会调用operator const Point&() const,问题来了operator const Point&() const要求返回一个Point对象引用,很明显return *this是Complex对象,那么它又会去调用operator const Point&() const返回一个Point对象引用。。。
|