007 operator

Posted huafan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了007 operator相关的知识,希望对你有一定的参考价值。

 

/*
目录:
   一 CComplex 类内
   二 CComplex 类外
   三 仿写std::cin和std::cout
*/

 

 

结构体对各种符号操作的重写设计,目的是扩展结构体/类的操作

 

一 CComplex 类内

#include "stdafx.h"
#include <iostream>

using namespace std;

class CComplex

public:
    CComplex()
    
        m_fImag = m_fReal = 1;
    
    CComplex(double r, double i):m_fReal(r), m_fImag(i)
    
    

    void Print()
    
        cout << m_fReal << "," << m_fImag << endl;
    
    operator double()
    
        return m_fReal;
    

    CComplex& operator!();
    CComplex& operator--();        // --i
    CComplex operator--(int);    // i--
    CComplex& operator++();        // ++i
    CComplex operator++(int);    // i++

    CComplex operator+(const CComplex &c2);
    CComplex operator-(const CComplex &c2);
    CComplex operator*(const CComplex &c2);
    CComplex operator/(const CComplex &c2);

private:
    double m_fReal;
    double m_fImag;
;

CComplex& CComplex::operator!()

    m_fReal = !m_fReal;
    m_fImag = !m_fImag;
    return *this;


CComplex& CComplex::operator--()

    --m_fReal;
    --m_fImag;
    return *this;


CComplex CComplex::operator--(int)

    CComplex temp = *this;
    --m_fReal;
    --m_fImag;
    return temp;


CComplex& CComplex::operator++()

    ++m_fReal;
    ++m_fImag;
    return *this;


CComplex CComplex::operator++(int)

    CComplex temp = *this;
    ++m_fReal;
    ++m_fImag;
    return temp;



CComplex CComplex::operator+(const CComplex &c2)

    CComplex temp;
    temp.m_fReal = this->m_fReal + c2.m_fReal;
    temp.m_fImag = this->m_fImag + c2.m_fImag;
    return temp;


CComplex CComplex::operator-(const CComplex &c2)

    CComplex temp;
    temp.m_fReal = this->m_fReal - c2.m_fReal;
    temp.m_fReal = this->m_fReal - c2.m_fReal;
    return temp;


CComplex CComplex::operator*(const CComplex &c2)

    CComplex temp;
    temp.m_fReal = this->m_fReal * c2.m_fReal;
    temp.m_fReal = this->m_fReal * c2.m_fReal;
    return temp;


CComplex CComplex::operator/(const CComplex &c2)

    CComplex temp;
    temp.m_fReal = this->m_fReal / c2.m_fReal;
    temp.m_fReal = this->m_fReal / c2.m_fReal;
    return temp;


int main(int argc, char *argv[], char **envp)

    CComplex c1(2, 3), c2(4, 5);
    CComplex c3 = c1 + (++c2);

    double d = c3;    // 类型重载
    c3.Print();

    CComplex c4;
    c4 = !c3;
    c4.Print();

    return 0;

 

二 CComplex 类外

#include "stdafx.h"
#include <iostream>

using namespace std;

class CComplex

public:
    CComplex()
    
        m_fImag = m_fReal = 1;
    
    CComplex(double r, double i):m_fReal(r), m_fImag(i)
    
    

    void Print()
    
        cout << m_fReal << "," << m_fImag << endl;
    
    operator double()
    
        return m_fReal;
    

    friend CComplex& operator!(CComplex &c1);
    friend CComplex& operator--(CComplex &c1);        // --i
    friend CComplex operator--(CComplex &c1, int);    // i--
    friend CComplex& operator++(CComplex &c1);        // ++i
    friend CComplex operator++(CComplex &c1, int);    // i++

    friend CComplex operator+(const CComplex &c1, const CComplex &c2);
    friend CComplex operator-(const CComplex &c1, const CComplex &c2);
    friend CComplex operator*(const CComplex &c1, const CComplex &c2);
    friend CComplex operator/(const CComplex &c1, const CComplex &c2);

private:
    double m_fReal;
    double m_fImag;
;

CComplex& operator!(CComplex &c1)

    c1.m_fReal = !c1.m_fReal;
    c1.m_fImag = !c1.m_fImag;
    return c1;


CComplex& operator--(CComplex &c1)

    --c1.m_fReal;
    --c1.m_fImag;
    return c1;


CComplex operator--(CComplex &c1, int)

    CComplex temp = c1;
    --c1.m_fReal;
    --c1.m_fImag;
    return temp;


CComplex& operator++(CComplex &c1)

    ++c1.m_fReal;
    ++c1.m_fImag;
    return c1;


CComplex operator++(CComplex &c1, int)

    CComplex temp = c1;
    ++c1.m_fReal;
    ++c1.m_fImag;
    return temp;



CComplex operator+(const CComplex &c1, const CComplex &c2)

    CComplex temp;
    temp.m_fReal = c1.m_fReal + c2.m_fReal;
    temp.m_fImag = c1.m_fImag + c2.m_fImag;
    return temp;


CComplex operator-(const CComplex &c1, const CComplex &c2)

    CComplex temp;
    temp.m_fReal = c1.m_fReal - c2.m_fReal;
    temp.m_fReal = c1.m_fReal - c2.m_fReal;
    return temp;


CComplex operator*(const CComplex &c1, const CComplex &c2)

    CComplex temp;
    temp.m_fReal = c1.m_fReal * c2.m_fReal;
    temp.m_fReal = c1.m_fReal * c2.m_fReal;
    return temp;


CComplex operator/(const CComplex &c1, const CComplex &c2)

    CComplex temp;
    temp.m_fReal = c1.m_fReal / c2.m_fReal;
    temp.m_fReal = c1.m_fReal / c2.m_fReal;
    return temp;



int main(int argc, char *argv[], char **envp)

    CComplex c1(2, 3), c2(4, 5);
    CComplex c3 = c1 + (++c2);

    double d = c3;    // 类型重载
    c3.Print();

    CComplex c4;
    c4 = !c3;
    c4.Print();

    return 0;

 


三 仿写std::cin和std::cout

  压缩包: 链接

 

没系统 std::cin 和std::cout好用
        1 兼容性
        2 支持少(endl)

 

以上是关于007 operator的主要内容,如果未能解决你的问题,请参考以下文章

RW007系列综合实战1STM32+RW007实现BLE透传功能

RW007系列连载1RW007-BLE主机通信使用学习示例与综合Demo

RW007系列综合实战2使用RW007模块连接小米蓝牙温湿度计2代

007.pandas.to_datetime()

007.Numpy的梯度函数

007.pandas.to_datetime()