1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
class CVector {
public:
int x, y ;
const char* name ;
CVector( const char* name = "" ) : CVector(0,0,name) {};
CVector( int a, int b, const char* its_name = "" ) : x(a), y(b), name(its_name) {
std::cout << "construct CVector " << name << " {" << x << ',' << y << "}\n" ;
}
CVector operator + (const CVector&);
CVector& operator = ( const CVector& that ) {
std::cout << "assign CVector " << that.name << " {" << that.x << ',' << that.y
<< "} to CVector " << name << " (operator=)\n" ;
x = that.x ;
y = that.y ;
return *this ;
}
};
CVector CVector::operator+ (const CVector& param) {
std::cout << "CVector " << name << " {" << x << ',' << y << "} + CVector "
<< param.name << " {" << param.x << ',' << param.y << "} (operator+)\n\t" ;
CVector temp( x + param.x, y + param.y, "temp") ;
std::cout << "\treturn temp\n" ;
return temp ;
}
int main () {
CVector foo (3,1,"foo");
CVector bar (1,2,"bar");
CVector result("result");
std::cout << "\n........\nevaluate result = foo + bar;\n..........\n" ;
result = foo + bar;
std::cout << "........\n\n" ;
std::cout << result.x << ',' << result.y << '\n';
}
|