在 C++ 中使用重载时,如何将重载函数放在 lhs 中?
When using overloading in C++, how can I put the overloading function in the lhs?
我是C++的新手。我想定义一个class'ap_uint'。目标是方便地实现按位操作。
例如:
我在 class 'ap_uint' 中定义了一个 unsigned int 值。
我想实现读和写如下。
my_ap_uint(4, 2) = 7; // write operation, only set bits[4:2] to 3'b111
unsigne int data = my_ap_uint(4, 2); // read operation, slice bits[4:2] and assign it to data.
现在,我可以实现读操作了,但是不知道如何实现写操作。
我可以定义一个函数ap_uint::set(high_bit, low_bit, rhs),但是它不符合我的要求。我想使用 'my_ap_uint(4, 2) = 7;'
提前致谢!
#include "stdio.h"
#include "my_uint.h"
int main()
{
unsigned int data;
ap_uint my_ap_uint;
// manually set bit[4:2] to 3'b111
my_ap_uint.tmp = 0x0000001c;
data = my_ap_uint(4, 2);
printf("my_type1->tmp = %08x\n", data);
// use function to set bit[4:1] to 4'b1111
my_ap_uint.set(4, 1, 15);
data = my_ap_uint(4, 1);
printf("my_type1->tmp = %08x\n", data);
// target to put my_ap_unit(4, 1) to the left hand side
my_ap_uint(4, 1) = 0xff;
data = my_ap_uint(4, 1);
printf("my_type1->tmp = %08x\n", data);
printf("All DONE\n");
return 0;
}
my_unit.h
class ap_uint
{
public:
unsigned int tmp;
// constructor
ap_uint(){
tmp = 0;
}
// slice the bit[b:a]
unsigned int range(int b, int a){
unsigned tmp1;
unsigned tmp2;
tmp1 = tmp >> a;
tmp2 = (1 << (b-a+1))-1;
return tmp1&tmp2;
}
// overloading () with range() function
unsigned int operator() (int Hi, int Lo){
return this->range(Hi, Lo);
}
// manually set bit[b:a] = rhs
void set(int b, int a, unsigned int rhs){
unsigned int hi;
unsigned int mi;
unsigned int lo;
hi = (tmp >> (b+1)) << (b+1);
lo = (tmp << (32-a)) >> (32-a);
mi = rhs << a;
tmp = hi | lo | mi;
}
};
目前您已经在 =
运算符的左侧调用了 ()
运算符。这个 returns 一个你拥有的 unsigned int。然后,您尝试在您的示例中将此 unsigned int 设置为等于 7,这对您的 ap_uint
对象没有任何影响。
尝试使用另一个函数 returns 一个不同的对象,该对象在特定范围内引用 ap_uint
对象内的数据,然后为该新对象重载 =
运算符。
您可能 return 代理 class:
class ap_uint
{
struct Proxy
{
ap_uint* parent = nullptr;
int hi;
int lo;
Proxy& operator =(unsigned u) {
parent->set(hi, lo, u);
return *this;
}
operator unsigned int () const {
return parent->range(hi, lo);
}
};
public:
Proxy operator() (int Hi, int Lo) {
return {this, Hi, Lo};
}
// ...
};
我是C++的新手。我想定义一个class'ap_uint'。目标是方便地实现按位操作。 例如: 我在 class 'ap_uint' 中定义了一个 unsigned int 值。 我想实现读和写如下。
my_ap_uint(4, 2) = 7; // write operation, only set bits[4:2] to 3'b111
unsigne int data = my_ap_uint(4, 2); // read operation, slice bits[4:2] and assign it to data.
现在,我可以实现读操作了,但是不知道如何实现写操作。 我可以定义一个函数ap_uint::set(high_bit, low_bit, rhs),但是它不符合我的要求。我想使用 'my_ap_uint(4, 2) = 7;'
提前致谢!
#include "stdio.h"
#include "my_uint.h"
int main()
{
unsigned int data;
ap_uint my_ap_uint;
// manually set bit[4:2] to 3'b111
my_ap_uint.tmp = 0x0000001c;
data = my_ap_uint(4, 2);
printf("my_type1->tmp = %08x\n", data);
// use function to set bit[4:1] to 4'b1111
my_ap_uint.set(4, 1, 15);
data = my_ap_uint(4, 1);
printf("my_type1->tmp = %08x\n", data);
// target to put my_ap_unit(4, 1) to the left hand side
my_ap_uint(4, 1) = 0xff;
data = my_ap_uint(4, 1);
printf("my_type1->tmp = %08x\n", data);
printf("All DONE\n");
return 0;
}
my_unit.h
class ap_uint
{
public:
unsigned int tmp;
// constructor
ap_uint(){
tmp = 0;
}
// slice the bit[b:a]
unsigned int range(int b, int a){
unsigned tmp1;
unsigned tmp2;
tmp1 = tmp >> a;
tmp2 = (1 << (b-a+1))-1;
return tmp1&tmp2;
}
// overloading () with range() function
unsigned int operator() (int Hi, int Lo){
return this->range(Hi, Lo);
}
// manually set bit[b:a] = rhs
void set(int b, int a, unsigned int rhs){
unsigned int hi;
unsigned int mi;
unsigned int lo;
hi = (tmp >> (b+1)) << (b+1);
lo = (tmp << (32-a)) >> (32-a);
mi = rhs << a;
tmp = hi | lo | mi;
}
};
目前您已经在 =
运算符的左侧调用了 ()
运算符。这个 returns 一个你拥有的 unsigned int。然后,您尝试在您的示例中将此 unsigned int 设置为等于 7,这对您的 ap_uint
对象没有任何影响。
尝试使用另一个函数 returns 一个不同的对象,该对象在特定范围内引用 ap_uint
对象内的数据,然后为该新对象重载 =
运算符。
您可能 return 代理 class:
class ap_uint
{
struct Proxy
{
ap_uint* parent = nullptr;
int hi;
int lo;
Proxy& operator =(unsigned u) {
parent->set(hi, lo, u);
return *this;
}
operator unsigned int () const {
return parent->range(hi, lo);
}
};
public:
Proxy operator() (int Hi, int Lo) {
return {this, Hi, Lo};
}
// ...
};