C++ 具有重写运算符的模板类
我想添加一个操作符覆盖来执行赋值/\uuu设置\uu的内联操作 模板:-C++ 具有重写运算符的模板类,c++,templates,operators,C++,Templates,Operators,我想添加一个操作符覆盖来执行赋值/\uuu设置\uu的内联操作 模板:- class CBase { public : static void SetupVmeInterface(CVmeInterface *in); protected : static CVmeInterface *pVmeInterface; }; template <class T> class TCVmeAccess : public CBase {
class CBase {
public :
static void SetupVmeInterface(CVmeInterface *in);
protected :
static CVmeInterface *pVmeInterface;
};
template <class T> class TCVmeAccess : public CBase {
public:
TCVmeAccess(int address);
T get()
{
unsigned long temp = pVmeInterface->ReadAddress(Address);
T ret = *reinterpret_cast<T*>(&temp);
return ret;
};
T *operator->();
unsigned long asLong();
bool set(T data)
{
unsigned long write_data = *reinterpret_cast<unsigned long*>(&data);
return pVmeInterface->WriteAddress(Address, write_data);
};
// void operator->(T);
void operator=(T data)
{ set(data); }
private :
int Address;
};
typedef struct
{
int a: 1; // 0
int b: 1; // 1
int c: 1; // 2
int d: 1; // 3
int NotUsed : 28; // 31-4
} _HVPSUControl;
代码体:-
TCVmeAccess<_HVPSUControl> HVPSUControl(constHVPSUControlBlock);
_HVPSUControl hvpsu = HVPSUControl.get(); // Yep, good, but not as nice as...
int a = HVPSUControl2.get().OperationalRequestPort; // yep, also good, but...
int b = HVPSUControl->a; // works, and is all go so far
HVPSUControl.set(hvpsu); // works, but need _HVPSUControl type
HVPSUControl = hvpsu; // also works, as operator = is used, but still need type
// this line does not work!
// as the = assignment is redirected into a copy of the struct, not the template
HVPSUControl->a = 1; // this line
我想做什么
HVPSUControl.a = 1; // or
HVPSUControl->a = 1; // or ?
作为在线工作:
如果(hvpuscontrol->a)而不是覆盖“->”和“=”运算符,则可以从模板结构派生
template <class T> class TCVmeAccess : public CBase, public T {
public:
TCVmeAccess(int address);
T get();
// T *operator->();
unsigned long asLong();
bool set(T);
// void operator->(T);
// void operator=(T);
private :
int Address;
};
HVPSUControl.a = 1; // and use this for setting a bitfield.
或
你不能让这条线以任何其他方式工作。您可以调用
TCVmeAccess::operator->
,它返回一个\u HVPSUControl*
,然后访问\u HVPSUControl*::a
还是要将整数1分配给完整的\u hvpuscontrol
结构
template <class T> class TCVmeAccess : public CBase {
public:
// ...
void operator=(T data) { set(data); }
void operator=(int n) { operator=(T(n)); }
// ...
};
typedef struct
{
_HVPSUControl(int n) {
*this = reinterpret_cast<_HVPSUControl>(n);
}
int a: 1; // 0
int b: 1; // 1
int c: 1; // 2
int d: 1; // 3
int NotUsed : 28; // 31-4
} _HVPSUControl;
你想要的行为是什么?你实际上想要做什么?为什么错误让你感到惊讶?我希望“这一行”像模板类中的“集合”那样执行。顺便说一句,没有“错误”,它只是不“做任何事情”(它不通过模板=)在我看来,
操作符->
返回一个T*,然后那里只有一个a
。a
可能被编译为operator->
的参数,但我不明白您如何使用它,因为它实际上不是一个变量或任何东西。问题是它会将“1”本地分配给“HVPSUControl”的实例,然而我需要这个赋值来通过我的类中的一个访问器方法(我需要对它进行一些处理)啊,只要阅读文本“dervied”部分中的“public T…”,就会播放…不,它只会设置结构的本地实例。我需要一个赋值来通过template类中的访问器。Close,虽然不得不从基本结构派生出我所有的(40+)结构是一件很遗憾的事情,但是保存内联代码的代价很小。。。如果能成功的话!我将玩它…赋值不知道为哪个结构成员赋值。我想我想要的是做不到的!快速回复“我是否要将整数1分配给所有”=否!我想从模板中提取结构成员,为其分配一些数据。但是这个作业需要通过模板类,这样我就可以用它做一些事情了。
struct _HVPSUControl
{
int a: 1; // 0
int b: 1; // 1
int c: 1; // 2
int d: 1; // 3
int NotUsed : 28; // 31-4
void operator = (int x);
};
struct _HVPSUBase {
void operator = (int x);
}
struct _HVPSUControl: public _HVPSUBase
{
int a: 1; // 0
int b: 1; // 1
int c: 1; // 2
int d: 1; // 3
int NotUsed : 28; // 31-4
};
template <class T> class TCVmeAccess : public CBase {
public:
// ...
void operator=(T data) { set(data); }
void operator=(int n) { operator=(T(n)); }
// ...
};
typedef struct
{
_HVPSUControl(int n) {
*this = reinterpret_cast<_HVPSUControl>(n);
}
int a: 1; // 0
int b: 1; // 1
int c: 1; // 2
int d: 1; // 3
int NotUsed : 28; // 31-4
} _HVPSUControl;
HVPSUControl = 1;