在基于模板的 class 中重载赋值运算符
Overloading Assignment operator in template based class
我正在编写一个库来支持一种具有两个模板参数 INT_BITS
和 FRAC_BITS
的整数。我成功地编写了一个转换函数,将不同的 class 类型从一种类型转换为另一种类型 [ INT_BITS
和 FRAC_BITS
的值各不相同]。但是当我尝试在赋值运算符的重载中使用它时,它不起作用。请建议我一种实现它的方法。我浏览了链接 here here and here,但 none 的解决方案似乎有效。
Class定义:
template<int INT_BITS, int FRAC_BITS>
struct fp_int
{
public:
static const int BIT_LENGTH = INT_BITS + FRAC_BITS;
static const int FRAC_BITS_LENGTH = FRAC_BITS;
private:
ValueType stored_val;
};
转换函数定义:
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
fp_int<INT_BITS_NEW, FRAC_BITS_NEW> convert() const
{
typedef typename fp_int<INT_BITS_NEW, FRAC_BITS_NEW>::ValueType TargetValueType;
return fp_int<INT_BITS_NEW, FRAC_BITS_NEW>::createRaw(
CONVERT_FIXED_POINT<
ValueType,
TargetValueType,
(FRAC_BITS_NEW - FRAC_BITS),
(FRAC_BITS_NEW > FRAC_BITS)
>:: exec(stored_val));
}
运算符定义如下:
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
fp_int<INT_BITS_NEW, FRAC_BITS_NEW>
operator =(fp_int<INT_BITS,FRAC_BITS> value) const
{
fp_int<INT_BITS_NEW,FRAC_BITS_NEW> a = value.convert<INT_BITS_NEW,FRAC_BITS_NEW>();
return a;
}
当我尝试这个时它起作用了:
fp_int<8,8> a = 12.4;
fp_int<4,4> b = a.convert<4,4>();
但是当我尝试这样做时它显示类型转换错误:
fp_int<8,8> a = 12.4;
fp_int<4,4> b;
b = a;
请告诉我哪里错了。
假设您使用的是普通 classes,而不是模板。您有一个 class SomeType
并且您希望为此 class 分配一个赋值运算符,以便您可以将类型 OtherType
的对象分配给此 class 的对象。所以像这样:
SomeType obj1;
OtherType obj2;
obj1 = obj;
要实现这一点,您可以像这样为 SomeType
编写赋值运算符:
SomeType& operator=(const OtherType& other)
{
// implementation...
return *this;
}
将其转换为模板,SomeType
和 OtherType
是同一模板 class 的实例化,但具有不同的模板参数。
在这种情况下 SomeType
变成 fp_int<INT_BITS, FRAC_BITS>
而 OtherType
变成类似 fp_int<DIFFERENT_INT_BITS, DIFFERENT_FRAC_BITS>
.
因此您的运算符应如下所示:
template <int DIFFERENT_INT_BITS, int DIFFERENT_FRAC_BITS>
fp_int<INT_BITS, FRAC_BITS>&
operator =(fp_int<DIFFERENT_INT_BITS, DIFFERENT_FRAC_BITS> value)
{
// proper implementation for an assignment operator
}
将上面的模板参数与您示例中的模板参数进行比较,看看有何不同。基本上你试图以错误的方向进行转换,这就是为什么你会收到有关类型转换的编译错误。
我正在编写一个库来支持一种具有两个模板参数 INT_BITS
和 FRAC_BITS
的整数。我成功地编写了一个转换函数,将不同的 class 类型从一种类型转换为另一种类型 [ INT_BITS
和 FRAC_BITS
的值各不相同]。但是当我尝试在赋值运算符的重载中使用它时,它不起作用。请建议我一种实现它的方法。我浏览了链接 here here and here,但 none 的解决方案似乎有效。
Class定义:
template<int INT_BITS, int FRAC_BITS>
struct fp_int
{
public:
static const int BIT_LENGTH = INT_BITS + FRAC_BITS;
static const int FRAC_BITS_LENGTH = FRAC_BITS;
private:
ValueType stored_val;
};
转换函数定义:
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
fp_int<INT_BITS_NEW, FRAC_BITS_NEW> convert() const
{
typedef typename fp_int<INT_BITS_NEW, FRAC_BITS_NEW>::ValueType TargetValueType;
return fp_int<INT_BITS_NEW, FRAC_BITS_NEW>::createRaw(
CONVERT_FIXED_POINT<
ValueType,
TargetValueType,
(FRAC_BITS_NEW - FRAC_BITS),
(FRAC_BITS_NEW > FRAC_BITS)
>:: exec(stored_val));
}
运算符定义如下:
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
fp_int<INT_BITS_NEW, FRAC_BITS_NEW>
operator =(fp_int<INT_BITS,FRAC_BITS> value) const
{
fp_int<INT_BITS_NEW,FRAC_BITS_NEW> a = value.convert<INT_BITS_NEW,FRAC_BITS_NEW>();
return a;
}
当我尝试这个时它起作用了:
fp_int<8,8> a = 12.4;
fp_int<4,4> b = a.convert<4,4>();
但是当我尝试这样做时它显示类型转换错误:
fp_int<8,8> a = 12.4;
fp_int<4,4> b;
b = a;
请告诉我哪里错了。
假设您使用的是普通 classes,而不是模板。您有一个 class SomeType
并且您希望为此 class 分配一个赋值运算符,以便您可以将类型 OtherType
的对象分配给此 class 的对象。所以像这样:
SomeType obj1;
OtherType obj2;
obj1 = obj;
要实现这一点,您可以像这样为 SomeType
编写赋值运算符:
SomeType& operator=(const OtherType& other)
{
// implementation...
return *this;
}
将其转换为模板,SomeType
和 OtherType
是同一模板 class 的实例化,但具有不同的模板参数。
在这种情况下 SomeType
变成 fp_int<INT_BITS, FRAC_BITS>
而 OtherType
变成类似 fp_int<DIFFERENT_INT_BITS, DIFFERENT_FRAC_BITS>
.
因此您的运算符应如下所示:
template <int DIFFERENT_INT_BITS, int DIFFERENT_FRAC_BITS>
fp_int<INT_BITS, FRAC_BITS>&
operator =(fp_int<DIFFERENT_INT_BITS, DIFFERENT_FRAC_BITS> value)
{
// proper implementation for an assignment operator
}
将上面的模板参数与您示例中的模板参数进行比较,看看有何不同。基本上你试图以错误的方向进行转换,这就是为什么你会收到有关类型转换的编译错误。