C++运算符重载

                      C++运算符的重载

  本人刚接触C++,学习时间不长,但是最近在设计class遇到一个关于运算符重载的问题。虽然最后也是通过度娘很easy的解决了,但是我感觉这里有一个容易被人们忽略的细节问题,尤其是在设计class时。

  目前就我的学习水平来看,我吧运算符重载分为了两种。一种是公开的运算符重载,一种是被封装起来的运算符重载。下面我们通过代码和示例进行一下说明,本人道行甚浅,若有错误,还望斧正。

  一、公开的运算符重载:

    首先我们需要class一个Arrow类。

    这种运算符重载是在一个作用域内(scope)的全局重载。不会被封装进某一个class内。在class内部的运算符重载,由于重载时,重载的操作函数作为这个类的一个member function(成员函数)。所以会带一个this pointer(this 指针)作为默认参数。 

    

//!我们可以这样设计
class Arrow;

Arrow operator+(const Arrow& one, const Arrow& other);//这样没有了this pointer

Arrow Arrow::operator+(const Arrow& other);//这样函数会有默认参数 this pointer

 

  二、class(类)内的运算符重载:

    

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

class Arrow
{
public:
    Arrow(float x = 0, float y = 0, float z = 0)
        :m_x(x),m_y(y),m_z(z)
    {}
    Arrow(const Arrow& a)
    {
        m_x = a.x();
        m_y = a.y();
        m_z = a.z();
    }
    Arrow& operator=(const Arrow& a);
    Arrow& operator=(const double a[3]);
    float x() const {return m_x;}
    float y() const {return m_y;}
    float z() const {return m_z;}
    void setX(const float& x) {m_x = x;}
    void setY(const float& y) {m_y = y;}
    void setZ(const float& z) {m_z = z;}
    Arrow operator-(const Arrow& other);
    Arrow operator+(const Arrow& other);
    Arrow& operator*=(const double& rate);
    float operator*(const Arrow& other);

private:
    float m_x,m_y,m_z;
};

Arrow& Arrow::operator=(const Arrow& a)
{
    this->m_x = a.x();
    this->m_y = a.y();
    this->m_z = a.z();
    return *this;
}

float Arrow::operator*(const Arrow& other)
{
    float r = 0;
    r += this->m_x * other.x();
    r += this->m_y * other.y();
    r += this->m_z * other.z();
    return r;

}

Arrow& Arrow::operator*=(const double& rate)
{
    this->m_x *= rate;
    this->m_y *= rate;
    this->m_z *= rate;
    return *this;
}

Arrow Arrow::operator+(const Arrow& other)
{
    Arrow r(this->m_x + other.x(),
            this->m_y + other.y(),
            this->m_z + other.z());
    return r;
}

Arrow Arrow::operator-(const Arrow& other)
{
    Arrow r(this->m_x - other.x(),
            this->m_y - other.y(),
            this->m_z - other.z());
    return r;
}

Arrow& Arrow::operator=(const double a[3])
{
    this->m_x = a[0];
    this->m_y = a[1];
    this->m_z = a[2];
    return *this;
}

//!------------------------------------------------------------------------------------------------
int main()
{
    Arrow a1(1,2,3);
    Arrow a2(2,3,4);
    Arrow a3 = a1 + a2;
    float r = a1 * a2;
    Arrow a4 = a1 - a2;

    cout << "Hello world!" << endl;
    cout << "a1 = (" << a1.x() << "," << a1.y() << "," << a1.z() << ")" << endl;
    cout << "a2 = (" << a2.x() << "," << a2.y() << "," << a2.z() << ")" << endl;
    cout << "a1 + a2 = (" << a3.x() << "," << a3.y() << "," << a3.z() << ")" << endl;
    cout << "a1 - a2 = (" << a4.x() << "," << a4.y() << "," << a4.z() << ")" << endl;

    cout << "a1 * a2 = " << r << endl;
    a4 *= 4;
    cout << "a1 = (" << a4.x() << "," << a4.y() << "," << a4.z() << ")" << endl;
    return 0;
}

   三、关于C++ this pointer的一些补充:

      

         

complex c1,c2;

cout << c1.real() << endl;

cout << c2.real() << endl;

//!等同于

cout << complex::real(&c1) << endl;//!this pointer
cout << complex::real(&c2) << endl;

 

  

posted @ 2019-02-14 20:57  huobn  阅读(218)  评论(0编辑  收藏  举报