美文网首页
C++学习(4)运算符重载

C++学习(4)运算符重载

作者: su945 | 来源:发表于2020-08-12 20:36 被阅读0次

1.运算符重载

  • 运算符重载,就是对已有的运算符(C++中预定义的运算符)赋予多重的含义,使同一运算符作用于不同类型的数据时导致不同类型的行为。
  • 运算符重载的目的是:扩展 C++中提供的运算符的适用范围,使之能作用于对象。

 运算符重载的实质是函数重载
 可以重载为普通函数,也可以重载为成员函数
 把含运算符的表达式转换成对运算符函数的调用。
 把运算符的操作数转换成运算符函数的参数。
 运算符被多次重载时,根据实参的类型决定调用哪个运算符函数。
运算符重载的形式:
返回值类型 operator 运算符(形参表)
{
……
}


class Complex
{
public:
    double real, imag;
    Complex(double r = 0.0, double i = 0.0) :real(r), imag(i) { }
    Complex operator-(const Complex & c);
};
Complex operator+(const Complex & a, const Complex & b)
{
    return Complex(a.real + b.real, a.imag + b.imag); //返回一个临时对象
}
Complex Complex::operator-(const Complex & c)
{
    return Complex(real - c.real, imag - c.imag); //返回一个临时对象
}
int main()
{
    Complex a(4, 4), b(1, 1), c;
    c = a + b; //等价于c=operator+(a,b);
    cout << c.real << "," << c.imag << endl;
    cout << (a - b).real << "," << (a - b).imag << endl;
    //a-b等价于a.operator-(b)
    return 0;
}
/*
5,5
3,3
*/

2.赋值运算符重载

有时候希望赋值运算符两边的类型可以不匹配,比如,把一个int类型变量赋值给一个Complex对象,或把一个 char * 类型的字符串赋值给一个字符串对象,此时就需要重载赋值运算符“ =”。

  • 赋值运算符“=”只能重载为成员函数
class String {
private:
    char * str;
public:
    String() :str(new char[1]) { str[0] = 0; }
    const char * c_str() { return str; };
    String & operator = (const char * s);
    String::~String() { delete[] str; }
};
String & String::operator = (const char * s)
{ //重载“=”以使得 obj = “hello”能够成立
    delete[] str;
    str = new char[strlen(s) + 1];
    strcpy(str, s);
    return *this;
}
int main()
{
    String s;
    s = "Good Luck,"; //等价于 s.operator=("Good Luck,");
    cout << s.c_str() << endl;
    // String s2 = "hello!"; //这条语句要是不注释掉就会出错
    s = "Shenzhou 8!"; //等价于 s.operator=("Shenzhou 8!");
    cout << s.c_str() << endl;
    return 0;
}
/*
输出:
Good Luck,
Shenzhou 8!
*/

3.运算符重载为友元函数

  • 一般情况下,将运算符重载为类的成员函数,是较好的选择。
  • 但有时,重载为成员函数不能满足使用要求,重载为普通函数,又不能访问类的私有成员,所以需要将运算符重载为友元。
class Complex
{
    double real, imag;
public:
    Complex(double r, double i) :real(r), imag(i) { };
    Complex operator+(double r);
    friend Complex operator + (double r, const Complex & c);
};

4.运算符重载的注意事项

  1. C++不允许定义新的运算符 ;
  2. 重载后运算符的含义应该符合日常习惯;
     complex_a + complex_b
     word_a > word_b
     date_b = date_a + n
  3. 运算符重载不改变运算符的优先级;
  4. 以下运算符不能被重载:“ .” 、“ .*” 、“ ::” 、“ ?:” 、 sizeof;
  5. 重载运算符()、 []、 ->或者赋值运算符=时,运算符重载函数必须声明为
    类的成员函数。

相关文章

  • 第十一章 使用类

    运算符重载 运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。...

  • 1.2.15_C++ 关系运算符重载

    C++ 重载运算符和重载函数 C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),...

  • C++ 运算符重载

    运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。实际上,很多C++运算符已经重载。将*运算符用...

  • C++运算符重载

    C++运算符重载的实质:运算符重载的实质就是函数重载或函数多态。运算符重载是一种形式的C++多态。目的在于让人能够...

  • C++运算符重载-下篇 (Boolan)

    C++运算符重载-下篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++运算符重载-上篇 (Boolan)

    C++运算符重载-上篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++ 重载运算符

    C++重载运算符

  • C++重载

    重载 C++语言规定: 重载的运算符要保持原运算符的意义。只能对已有的运算符重载,不能增加新的运算符。重载的运算符...

  • 2017 09 22

    Am:对运算符重载进行学习: 基础定义部分: 1.重载定义:根据自己的需要对C++已提供的运算符进行重载,赋予新的...

  • 1.2.17_C++ ++ 和 -- 运算符重载

    C++ 重载运算符和重载函数 递增运算符( ++ )和递减运算符( -- )是 C++ 语言中两个重要的一元运算符...

网友评论

      本文标题:C++学习(4)运算符重载

      本文链接:https://www.haomeiwen.com/subject/bvuudktx.html