#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 << " ![]() 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; } |
原帖由 alexhappy 于 2009-3-12 10:09 发表
晕菜,你写的什么。。。
仔细看看你自个儿的代码:a()首先调用const Point& operator()() const,而在const Point& operator()() const这里面会调用operator const Point&() const,问题来了
operator cons ...
operator const Point&() const |
operator Point() const { return Point(iReal, iImaginary); } |
operator const Point&() const { cout << "operator const Point&() const" << endl; return *(reinterpret_cast< ![]() } |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |