【问题标题】:template class with overridden operators具有重写运算符的模板类
【发布时间】:2009-12-09 13:54:45
【问题描述】:

我想添加一个运算符覆盖来执行 assignments/__set__s 内联。

模板:-

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

那么,有没有办法让上面的这条线起作用?

编辑: 就像在模板类中一样,我希望“这条线”作为“集合”执行。

编辑: 1. 直接内联赋值给构成模板的结构的成员 的。
2. 使该分配通过模板访问器。

这样我就不必在作业中这样做了:-

// HVPSUControl is predefined and used many times.
_HVPSUControl hvpsu;
hvpsu.a = 1;
HVPSUControl.set(hvpsu);

我想做

HVPSUControl.a = 1; // or 
HVPSUControl->a = 1; // or ?

上线工作:

如果 (HVPSUControl->a)

【问题讨论】:

  • 你想要的行为是什么?
  • 你真正想要做什么以及为什么错误让你感到惊讶
  • 我希望“这条线”像模板类中的“集合”一样执行。
  • 顺便说一句,“没有错误”,它只是没有“做任何事情”(它不通过模板=)
  • 在我看来operator-&gt; 正在返回一个 T*,然后那里只有一个 a。 a 可能被编译为 operator-&gt; 的参数,但我不明白你将如何使用它,因为它不是真正的变量或任何东西。

标签: c++ templates operators


【解决方案1】:

您可以从模板结构派生,而不是覆盖“->”和“=”运算符。

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.

编辑:如果你想使用自定义赋值运算符,你应该在 HVPSUControl 中声明它,或者甚至是它的基类,如果你有更多类似控件的结构。

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
};

【讨论】:

  • 这个问题是它会在本地将“1”分配给“HVPSUControl”的实例,而我需要通过我的类中的访问器方法进行分配(我需要做一些处理就可以了)
  • 啊,只需阅读文本“dervied”部分中的“, public T”...,将播放...
  • 不,它只会设置结构的本地实例。我需要一个分配来通过模板类中的访问器。
  • 关闭,虽然不得不从一个基本结构派生我所有的(40+)个结构是很遗憾的,但保存内联代码将是一个很小的代价......如果它可以工作的话!我会玩它...
  • 赋值不知道哪个结构成员被赋值。我觉得我想做的事做不到!
【解决方案2】:

你不能让这条线以任何其他方式工作。你调用TCVmeAccess::operator-&gt;,它返回一个_HVPSUControl*,然后你访问_HVPSUControl*::a。

还是要将整数 1 分配给完整的 _HVPSUControl 结构?

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;

【讨论】:

  • 快速回复“我想将整数 1 分配给所有人”= 不!我想从模板中提取结构成员,为其分配一些数据。但是这个任务需要通过模板类,所以我可以用它做一些事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多