包装数字值

时间:2014-04-05 13:41:16

标签: c++ operator-overloading

我正在尝试编写一个包装数值的C ++程序,我这样做是通过编写一个处理两个简单函数的超类和一个运算符重载函数来完成的。这是我到目前为止的代码:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;



template <class T>
class Number {
protected:
    T number;

public:
    Number(T num) {
        number = num;
    }

    string mytype() {
        return typeid(number).name();
    }

    string what_am_i() {
        ostringstream oss;
        oss << "I am " << Number<T>::mytype() << " and my value is " << number;
        return oss.str();
    }

    Number operator+ (Number an) {
        Number brandNew = NULL;
        brandNew.number = number + an.number;
        return brandNew;
    }
};


class MyInt : public Number<int> {
public:
    MyInt() : Number<int>(0) {};
    MyInt(int num) : Number<int>(num) {}
    MyInt(const Number<int> &x) : Number<int>(x) {}
};



class MyFloat : public Number<float> {
public:
    MyFloat() : Number<float>(0){};
    MyFloat(float num) : Number(num){}
    MyFloat(const Number<float> &x) : Number<float>(x) {}   
};

class MyDouble : public Number<double> {
public:
    MyDouble() : Number<double>(0){};
    MyDouble(double num) : Number(num){}
    MyDouble(const Number<double> &x) : Number<double>(x) {}    
};

在主要功能中,我想做类似的事情:

void main() {       
    MyInt two = 2;       
    MyFloat flo = 5.0f;
    MyDouble md3 = flo + two;
}

并希望md3为15.00000,到目前为止,添加两个来自同一类型的对象效果很好但是当我尝试添加MyInt和MyFloat时,编译器并不喜欢它。有谁知道我怎么能实现这个?

2 个答案:

答案 0 :(得分:1)

您必须为模板类添加类型运算符:

operator T()
{
    return number;
}

这是我测试和工作的complate代码:

template <class T>
class Number {
protected:
    T number;

public:
    Number(T num) {
        number = num;
    }

    string mytype() {
        return typeid(number).name();
    }

    string what_am_i() {
        ostringstream oss;
        oss << "I am " << Number<T>::mytype() << " and my value is " << number;
        return oss.str();
    }

    Number operator+ (Number an) {
        Number brandNew = NULL;
        brandNew.number = number + an.number;
        return brandNew;
    }

    operator T()
    {
        return number;
    }
};

我试着更好地解释它为何起作用。当您对加号运算符进行重载时,您希望执行以下操作:left_value + right_value,其中right_value是&#34;&#34;加号运算符的参数。

现在要获得对象的正确价值&#34;&#34;,如果你不在你的内部超载这个操作符,你必须重载#34;类型操作符&#34;数字类它不能被读作正确的值。以下示例适用于operator =(),但对于operator +():

也有效
template<typename T>
class Number
{
  T value;

  public:

  T operator=(T arg) // Assignment, your class is seen as left operand
  {
    value = arg;
  }

  operator T() // Getting, your class is seen as right operand
  {
     return value;
  } 
}

Number<int>     A;  // define a new class Number as int
Number<double>  B;  // define a new class Number as double

A = B; // is same to A.operator=( B.double() );

将A指定为左值称为运算符&#34; operator =()&#34;类的数量,而将B作为正确的值称为&#34;运算符T()&#34;类号

现在翻译为:

// instance of the operator = of the class Number<int>
int operator=(int arg)
{
}


// instance of the Operator T of the class Number<double>
operator double()
{
}

这种翻译模拟了A和B对象的以下语义:

int A;
double B;

A = B;

侨 安吉洛

答案 1 :(得分:0)

您可以指定二元操作的结果并使用独立运算符:

#include <iostream>
#include <typeinfo>

template <class T>
class Number {
template <typename>
friend class Number;

protected:
    T number;

public:
    Number(const T& num)
    :   number(num)
    {}

    template <typename U>
    Number(const Number<U>& num)
    :   number(num.number)
    {}

    // ...

    const T& value() const { return number; }
};

typedef Number<int> MyInt;
typedef Number<float> MyFloat;
typedef Number<double> MyDouble;

template <typename A, typename B>
struct NumberResult;

template <typename X>
struct NumberResult<X, X> {
    typedef X type;
};

template <> struct NumberResult<int, float> { typedef float type; };
template <> struct NumberResult<float, int> { typedef float type; };
// ...

template <typename A, typename B>
inline Number<typename NumberResult<A, B>::type>
operator + (const Number<A>& a, const Number<B>& b) {
    return Number<typename NumberResult<A, B>::type>(a.value() + b.value());
}

注意:请避免标题中的using namespace std;