c++ 模版编程,operator重载

#pragma once
#include 
#include 
class half_t{
    uint16_t __x;

public:
    half_t():__x(0){}
    half_t(int a){
        *this=half_t(float(a));
    }
    half_t(const half_t&other){
        cout<<"拷贝构造函数..."<(other.__x);

    }
    half_t(float a){
        uint32_t ia=*reinterpret_cast(&a);
        uint16_t  ir;
        ir=(ia>>16)&0x8000;
        ir=0x14;
        cout<<"ir:"<__x=ir;
    }

    operator float()const{
        uint32_t f= 12;
        return reinterpret_cast(f);
        //return f;
    }

    half_t&operator +=(const half_t&rhs){
        *this=half_t(float(*this)+float(rhs));
        (*this).__x=8;
        return *this;

    }

    bool operator>=(const half_t &other) const
    {
        return float(*this) >= float(other);
    }


    uint32_t get(){

        return this->__x;
    }



};

int main() {
    float i=12;
    half_t a=half_t(12.f);
    half_t b(a);
    half_t *c=&a;
    cout<

Operator: 相关的重载函数

  1. std::ostream & operator <<(std::ostream & out ,const half_t &x){
    out<<(float)x;
    return out;
    }

  2. bool operator >=(const half_t & other) const{
    return float(*this) >= float(other);
    }

  3. half_t & operator +=(const half_t &rhs){
    *this =half_t(float(*this)+float(rhs));
    reuturn &=*this;
    }

  4. operator float() const{
    uint32_t f =0;
    return *reinterpret_cast(&f);
    }

  5. operator __half()const{
    Return reinterpret_cast(__x);
    }

你可能感兴趣的:(c++模版编程)