zoukankan      html  css  js  c++  java
  • C++面向对象高级编程(二)基础篇

    技术在于交流、沟通,转载请注明出处并保持作品的完整性。

    概要

    知识点1.重载成员函数

    知识点2 . return by value, return by reference

    知识点3 重载非成员函数

    知识点4 重载操作符


    这次主要回忆一下操作符重载.

    先看一段代码,后续我会介绍这么做的原由

    #include <iostream>
    class complex
    {
    public:
        complex(double r = 0, double i = 0) : re(r) ,im(i) {}
        complex& operator += (const complex& r);
        double real() const { return re; }
        double imag() const { return im; }
        void real(double r);
        
    private:
        double re,im;
        friend complex& __doapl (complex*, const complex&);
    };
    
    inline double
    imag(const complex& x)
    {
        return x.imag ();
    }
    inline double
    real(const complex& x)
    {
        return x.real ();
    }

    知识点1.重载成员函数

    inline complex&
    complex::operator += (const complex& r)
    {
        return __doapl (this, r);
    }

     C++的调用都是从左面开始,下面调用

    complex c1(1,2);
    complex c2(2,3);
    c1 += c2;

    例如c1 += c2 他的完整含义应该是 c1 调用了 +=这个函数 传递的参数是两个其中一个是this(c1),另一个参数就是右边的值了(c2),[在编译器里别这样写,编译会报错]

    //认识成员函数都有一个this point 指向调用者
    //+=的完整形式应该是这样,谁调用这个函数谁就是this
    inline complex&
    complex::operator+=(this,const complex& r)
    {
        return __dopal(this, r);
    }

    知识点2 . return by value, return by reference

    //2.return by reference
    inline complex& //引用接收 提高效率 还有一个重要的知识点 以备调用者 调用c3 += c2 += c1;
    __doapl(complex* ths, const complex& r)
    {
        ths->re += r.re;
        ths->im += r.im;
        return *ths; //返回的是对象, 接收却是引用,   这是C++的一个重要知识点,传递着无需知道接收者以什么形式接收
    }

    为什么 用引用接收  就可以让调用者调用c3 += c2 += c1;

    如果你不用引用接收那么你第一次调用即c3 += c2时返回的即将是一个临时变量,那么在次调用c1时  c3 += c2 将毫无意义 


    知识点3 重载非成员函数

    //3. 操作符重载 非成员函数 无this
    inline complex
    operator + (const complex& x, const complex& y)
    {
        return complex (real (x) + real (y),
                        imag (x) + imag (y));
    }
    inline complex
    operator + (const complex& x, double y)
    {
        return complex (real (x) + y, imag (x));
    }
    inline complex
    operator + (double x, const complex& y)
    {
        return complex (x + real (y), imag (y));
    }

    临时对象:typename() 创建临时对象 为什么上面的三个返回值不是reference 因为他们返回的一定是局部变量

    知识点2.是左边 = 左边+ 右边 左边是一直存在的


    知识点4 重载操作符

    //<< 重载  由于这个操作符不认识我们新创建的对象我们需要重载
    //千万不要把这个操作符写成成员函数 必须写成全局的
    std::ostream&
    operator << (std::ostream& os, const complex& x)  //这里的os就是cout 其实cout是一个类 别用const修饰  传引用为了能够相应连续调用
    {
        return os << '(' << real (x) << ','
        << imag (x) << ')';
    }

    总结 

    1.传递着无需知道接收者以什么形式接收

    2.接收者(返回值)是 by value还是 by reference,  by value 一般返回临时变量和将创建出的对象 , by reference 一般是在已经存在的对象上做修改

    3.操作符重载不要加const ,不要声明称成员函数

    如有不正确的地方请指正

    参照<<侯捷 C++面向对象高级编程>>

  • 相关阅读:
    2019.1.5JavaScript
    SQL常用删改增语句
    PHP连接数据库
    PHP数组函数
    PHP字符串常用函数
    PHP 类型判断方法
    jQuery效果
    jQuery特性
    倒计时
    判断浏览器及其内核
  • 原文地址:https://www.cnblogs.com/LearningTheLoad/p/7290019.html
Copyright © 2011-2022 走看看