运算符重载

最后更新于:2022-04-02 02:06:38

[TOC] ## 转换运算符 ``` class Fraction{ public: Fraction(int n,int d=1): m_Numerator(n), m_Denominator(d){} // 转 dobule operator double() const{ return (double)m_Numerator/m_Denominator; } // 转 QString operator QString() const{ return QString("%1/%2").arg(m_Numerator).arg(m_Denominator); } private: int m_Numerator,m_Denominator; }; QTextStream& operator<<(QTextStream& os,const Fraction& f) { os<<"operator<<"<(f); return os; } ``` 使用 ``` Fraction fra(4,3); double d=fra; QString str=fra; qDebug()<0); m_p=new int[m_size]; } int& operator[](int i){ assert(i ';