c/c++ 重载运算符的思考
程序员文章站
2023-01-13 13:47:50
c/c++ 重载运算符的思考 c++ include using namespace std; class Imaginary{ public: Imaginary():real(0), imag(0){ cout 分析:执行m3 = m1 + m2的时点,因为m3的空间已经创建过了,所以 立即 释 ......
c/c++ 重载运算符的思考
#include <iostream> using namespace std; class imaginary{ public: imaginary():real(0), imag(0){ cout << "c:" << this << endl; } imaginary(int real, int imag):real(real), imag(imag){ cout << "c:" << this << endl; } imaginary operator+ (const imaginary &m){ return imaginary (real + m.real, imag + m.imag); } ~imaginary(){ cout << this << endl; } private: int real; int imag; }; int main(){ imaginary m1(10, 20); imaginary m2(1, 2); imaginary m3; printf("m3 : %p\n",&m3); m3 = m1 + m2; cout << 1 << endl; return 0; }
执行结果: