简体   繁体   English

2个半字节结构,分配运算符和位集。 如何?

[英]2 Nibbles Struct, assign operator and bitset. How to?

I have an old device which send thru a serial port a large array of 7-bit bytes (the most significant bit is always 0). 我有一台旧设备,该设备通过串行端口发送7位字节的大型数组(最高有效位始终为0)。 These bytes are sent splitted in two nibbles, so a byte 0abcdefg is received as 00000abc 0000defg . 这些字节被分成两个半字节发送,因此接收到的字节0abcdefg00000abc 0000defg So i created a DoubleByte struct to store each of them: 所以我创建了一个DoubleByte结构来存储它们中的每一个:

struct Dbyte {
    BYTE h;
    BYTE l;
};

Now, i have 2 problems: 现在,我有2个问题:

1) what i want to do is to access each Dbyte as a normal, basic type variable, so i tried to override some operator: 1)我想做的是将每个Dbyte作为普通的基本类型变量进行访问,因此我尝试覆盖一些运算符:

struct Dbyte {
    BYTE h;
    BYTE l;
    Dbyte& operator =(const Dbyte& a) {
        l = a.l; h = a.h;
        return *this;
    }
    Dbyte& operator =(const int& i) {
        l = i&0x0F; h = i>>4 & 0x07;
        return *this;
    }
    int operator = (const Dbyte& a) const {
        return (int)(a.h<<4 + a.l);
    }
};

With my code i can do: 使用我的代码,我可以做到:

Dbyte db1, db2;
db1 = 20;
db2 = db1;

but i can't compile 但我不能编译

int i = db1;

What i'm doing wrong? 我做错了什么?

2) Some of these Dbytes are bitfields (or the low nibble is a value and the high nibble is a bitfield). 2)其中一些Dbytes是位字段(或者低半字节是一个值,高半字节是一个位字段)。 There's a handy way to access them as bit? 有一种方便的方式可以访问它们吗? I've just learned the existence of std::bitset but i still don't know how to use them, and in my dreams i would like to write something like 我刚刚了解了std :: bitset的存在,但我仍然不知道如何使用它们,在我的梦中,我想写些类似的东西

if (db1[6]==true) ...

or 要么

if (db1.bit[6]==true) ...

There's a way to obtain something like this? 有办法获得这样的东西吗?

If this matters, i'm working with C++Builder 2006, but the project could be migrated in a QTQuick app, so i'd prefere a "stardard c++" solution. 如果这很重要,那么我正在使用C ++ Builder 2006,但是该项目可以在QTQuick应用程序中进行迁移,因此我更喜欢“ stardard c ++”解决方案。

Thanks 谢谢

To convert your type to another, you need a conversion, not assignment, operator: 要将您的类型转换为其他类型,您需要一个转换(而不是赋值)运算符:

operator int() const {
    return (h<<4) + l;
}

For the opposite conversion, you would be better off with a converting constructor, rather than an assignment operator: 对于相反的转换,最好使用转换构造函数,而不是赋值运算符:

DByte(int i) : l(i & 0xf), h((i>>4) & 0xf) {}

This can be used for initialising a DByte as well as assigning to it: 这可以用于初始化DByte以及分配给它:

DByte db = 42;  // not assignment - requires a constructor

Assignment still works with just this constructor, and no special assignment operator - an integer value can be converted using the constructor, then assigned with the implicit assignment operator. 赋值仍然仅适用于此构造函数,而没有特殊的赋值运算符-可以使用构造函数转换整数值,然后使用隐式赋值运算符进行赋值。

Finally, there's no point writing your own copy constructor here. 最后,这里没有必要编写自己的副本构造函数。 The implicit one does exactly the same thing. 隐式的功能完全相同。

int operator = (const Dbyte& a) const tries to be a copy-assignment operator but can't be because it's const. int operator = (const Dbyte& a) const试图成为一个拷贝分配运算符,但不能因为它是const而成为。 What you wanted to write was: operator int() const 您要写的是: operator int() const

I think your overloaded = operator for int is out of scope because you placed it in the struct for DByte. 我认为您的int重载=运算符超出了范围,因为您将其放在了DByte的结构中。 For your Dbyte[i] == true concept, try overloading the [] operator to return a boolean. 对于您的Dbyte [i] == true概念,请尝试重载[]运算符以返回布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM