C++ C++;:是否有一种不那么冗长的方式来表示自定义数字类型?

C++ C++;:是否有一种不那么冗长的方式来表示自定义数字类型?,c++,operator-overloading,C++,Operator Overloading,我需要表示伏特、安培和瓦特及其关系(例如,W=V*I) 这是我带来的,但它似乎真的冗长。有没有办法让它更简洁 #include <stdio.h> #include <iostream> template<class T> class double_val { public: explicit double_val(double val):_val(val) { }; double_val(const T& other

我需要表示伏特、安培和瓦特及其关系(例如,W=V*I)

这是我带来的,但它似乎真的冗长。有没有办法让它更简洁

#include <stdio.h>
#include <iostream>


template<class T>    
class double_val {   
public:
    explicit double_val(double val):_val(val) { };
    double_val(const T& other) {
        _val = other._val;
    }
    T& operator=(const T &other) {
        _val = other._val;
        return *this;
    }
    T operator+ (const T& other) const {
        return T(this->_val + other._val);
    }
    T operator+ (double val) const {
        return T(this->_val + val);
    }
    T operator- (const T& other) const {
        return T(this->_val - other._val);
    }
    T operator- (double val) const {
        return T(this->_val - val);
    }
    T operator* (const T& other) const {
        return T(this->_val * other._val);
    }
    T operator* (double val) const {
        return T(this->_val * val);
    }
    T operator/ (const T& other) const {
        return T(this->_val / other._val);
    }
    T operator/ (double val) const {
        return T(this->_val / val);
    }
    bool operator== (const T& other) const{
        return this->_val == other._val;
    }
    bool operator!= (const T& other) const{
        return this->_val != other._val;
    }
    bool operator > (const T& other) const{
        return this->_val > other._val;
    }
    bool operator >= (const T& other) const{
        return this->_val >= other._val;
    }
    bool operator < (const T& other) const{
        return this->_val < other._val;
    }
    bool operator <= (const T& other) const{
        return this->_val <= other._val;
    }
    void val(double val){
        _val = val;
    }
    double val() const {
        return _val ;
    }
    virtual const char* name() const = 0;

private:
    double _val;
};
template<class T>
std::ostream& operator<<(std::ostream &os, const double_val<T> &t) {
    return os << t.val() <<  " " << t.name();
}


class Amper:public double_val<Amper> {
public:
    Amper(double val):double_val<Amper>(val) {}
    const char* name() const {
        return "Amper";
    }
};

class Volt:public double_val<Volt> {
public:
    Volt(double val):double_val<Volt>(val) {}
    const char* name() const {
        return "Volt";
    }
};

class Watt:public double_val<Watt> {
public:
    Watt(double val):double_val<Watt>(val) {}
    const char* name() const {
        return "Watt";
    }
};

// Watt = I * V
Watt operator* (const Volt &v, const Amper& a) {
    return Watt(a.val() * v.val());
}
Watt operator* (const Amper &a, const Volt& v) {
    return Watt(a.val() * v.val());
}

// Volts = Watts/Ampers
Volt operator / (const Watt &w, const Amper& a) {
    return Volt(w.val() / a.val());
}

// Ampers = Watts/Volts
Amper operator / (const Watt &w, const Volt& v) {
    return Amper(w.val() / v.val());
}



int main(int argc, char **argv) {
    using namespace std;
    Watt w = Volt(66) * Amper(7);
    Amper a = w / (Volt(646) * Volt(444));

    cout << a << endl;

    return 0;
}
#包括
#包括
模板
类double_val{
公众:
显式double_val(double val):_val(val){};
双值(常数T和其他){
_val=其他;
}
T和运算符=(常数T和其他){
_val=其他;
归还*这个;
}
T运算符+(常数T和其他)常数{
返回T(此->值+其他值);
}
T运算符+(双值)常数{
返回T(此->值+值);
}
T运算符-(常数T和其他)常数{
返回T(此->\u val-其他.\u val);
}
T运算符-(双值)常数{
返回T(this->u val-val);
}
T运算符*(常数T和其他)常数{
返回T(此->值*其他值);
}
T运算符*(双值)常数{
返回T(此->_val*val);
}
T运算符/(常数T和其他)常数{
返回T(此->值/其他值);
}
T运算符/(双值)常数{
返回T(此->值/val);
}
布尔运算符==(常数T和其他)常数{
返回此->\u val==other.\u val;
}
布尔运算符!=(常数T和其他)常数{
返回此->\u val!=其他。\u val;
}
布尔运算符>(常数T和其他)常数{
返回此->\u val>other.\u val;
}
布尔运算符>=(常数T和其他)常数{
返回此->\u val>=其他。\u val;
}
布尔运算符<(常数T和其他)常数{
返回此->\u valbool运算符_val在库形式中确实有一种不太详细的方法,将SI单位定义为类型安全类模板实例化


特别是,它定义了单元。

如果客户机代码协同工作,您可以使用删除逻辑操作。
rel_ops
根据==和<运算符定义一组逻辑运算符

然而,这是有问题的——它迫使客户端使用rel_ops,rel_ops突然让任何带有==和<运算符的类与所有其他运算符进行比较。这可能不是您想要的

在我看来,更好的选择是使用该库,它似乎是数值类型的理想选择,因为它为数值类型提供了您可能需要的所有各种运算符。您只需定义一些基本运算符,然后根据您的规范填充其余的运算符。要从boost文档的示例中吸取经验,请执行以下操作:

template <class T>
class point    // note: private inheritance is OK here!
    : boost::addable< point<T>          // point + point
    , boost::subtractable< point<T>     // point - point
    , boost::dividable2< point<T>, T    // point / T
    , boost::multipliable2< point<T>, T // point * T, T * point
      > > > >
{
public:
    point(T, T);
    T x() const;
    T y() const;

    point operator+=(const point&);
    // point operator+(point, const point&) automatically
    // generated by addable.

    point operator-=(const point&);
    // point operator-(point, const point&) automatically
    // generated by subtractable.

    point operator*=(T);
    // point operator*(point, const T&) and
    // point operator*(const T&, point) auto-generated
    // by multipliable.

    point operator/=(T);
    // point operator/(point, const T&) auto-generated
    // by dividable.
private:
    T x_;
    T y_;
};
模板
类点//注意:这里可以使用私有继承!
:boost::可添加<点//点+点
,boost::可减<点//点-点
,boost::dividable2 > > >
{
公众:
点(T,T);
tx()常数;
T y()常数;
点运算符+=(常数点&);
//点运算符+(点、常量点&)自动
//由addable生成。
点运算符-=(常数点&);
//点运算符-(点,常量点&)自动
//由可减法生成。
点算子*=(T);
//点运算符*(点,常数T&)和
//点运算符*(常数T和,点)自动生成
//以倍数计算。
点算子/=(T);
//自动生成的点运算符/(点,常数&)
//可分割的。
私人:
T x_;
T y_;
};
此外,还包含一些设施,可大大简化此类情况下所有操作员管道的编写


我最近在这里做了一个示例:

是一个很好的解决方案,可以满足您的需求,因为它提供了电压、电流、瓦特和开箱即用的适当操作员的响应


解决了这个问题,一般情况下,自定义数字类型不是SI单位。< /P>检查这个升压库:它做你需要的:单位转换,维度分析等。你看了Booost?单位看起来像是一个非常适合你的任务。像往常一样,在C++中,所有的方式导致Boost: