c/c++ 重载运算符的思考

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;
}

执行结果:

分析:执行m3 = m1 + m2的时点,因为m3的空间已经创建过了,所以立即释放了operator+中创建的对象,打印了1出来,最后才释放了其余3个对象。

如果代码改成:Imaginary m3 = m1 + m2; 因为m3还没有被创建过,所以不释放operator+中创建的对象。

原文地址:https://www.cnblogs.com/xiaoshiwang/p/9500880.html

时间: 2024-08-07 16:10:47

c/c++ 重载运算符的思考的相关文章

C++学习之重载运算符1

C++除可重载函数之后,还允许定义已有的运算符,这样通过运算符重载可像处理数据使用它们. 先来个代码 1 #include<iostream> 2 using namespace std; 3 4 class num 5 { 6 public: 7 num(){n=1;} 8 ~num(){} 9 int get() const{return n;} 10 void set(int x){n=x;} 11 private: 12 int n; 13 }; 14 15 int main() 16

矩阵求和--重载运算符

C++习题 矩阵求和--重载运算符 [Submit][Status][Web Board] Description 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运算符"+",使之能用于矩阵相加(如c=a+b). 重载流插入运算符"<<"和流提取运算符">>",使之能用于该矩阵的输入和输出. Input 两个2行3列矩阵 Output 矩阵之和 Sample Input 1 2 34 5 67 8 91 2 3 Sa

重载()运算符和重载强制类型转换

// 研究了半宿.最终弄清楚了 // 写了这段測试代码能够非常好的演示效果 class CConvert { public: CConvert(){m_nValue = 10;} // 重载()运算符 int operator ()(); // 重载int强制类型转换 operator int(); protected: private: int m_nValue; }; int CConvert::operator ()() { return m_nValue; } CConvert::ope

C++自学笔记_重载运算符_《C++ Primer》

#include <iostream> #include <stdexcept> using namespace std; class CheckedPtr{ public: CheckedPtr(int *b,int *e,int *c): beg(b),end(e),curr(c){ } CheckedPtr(const CheckedPtr &obj): //复制构造函数 beg(obj.beg),end(obj.end),curr(obj.curr){ } Chec

作为类的成员函数,重载运算符只能有一个参数

1 overload a operator of a class, you can only use one para., this pointer is automatically used. class Rational { public: //not correct since this ponit would be used automatically. //Rational operator+ (const Rational& lhs, const Rational& rhs);

C++习题 复数类--重载运算符+,-,*,/

Description 定义一个复数类Complex,重载运算符"+","-","*","/",使之能用于复数的加.减.乘.除.运算符重载函数作为Complex类的成员函数.编写程序,分别求两个复数之和.差.积和商. Input 两个复数 Output 两个复数之和.差.积和商 Sample Input 3 4 5 -10 Sample Output c1+c2=(8.00,-6.00i) c1-c2=(-2.00,14.00

operator重载运算符

1.重载运算符的函数一般格式如下 函数类型    operator  运算符名称    (形参表列) {对运算符的重载处理} 例如,想将“+”用于Complex(复数)的加法运算,函数的原型可以是这样的: Complex operator + (Complex & c1,Complex &c2); operator+函数表示对运算符+重载.其中,operator是关键字,专门用于定义重载运算符的函数的,运算符名称就是C++提供给用户的预定运算符. 注意:函数名是由operator和运算符组

C++学习27 用全局函数重载运算符

运算符重载函数既可以声明为类的成员函数,也可以声明为所有类之外的全局函数. 运算符重载函数作为类的成员函数 将运算符重载函数声明为类的成员函数时,二元运算符的参数只有一个,一元运算符不需要参数.之所以少一个参数,是因为这个参数是隐含的. 例如,上节的 complex 类中重载了加法运算符: complex operator+(const complex & A)const; 当执行: c3 = c1 + c2; 会被转换为: c3 = c1.operator+(c2); 通过 this 指针隐式

C# 重载运算符

如果你想让自己定义的类型可以用运算符进行运算,那么可以通过重载运算符来实现: 示例: class Salary { public int RMB { get; set; } public static Salary operator +(Salary s1, Salary s2) { return new Salary { RMB = s1.RMB + s2.RMB }; } } //调用 Salary mikeIncome = new Salary { RMB = 10 }; Salary r