相关疑难解决方法(0)

布尔值对位域的优点

我工作的代码库很旧。虽然我们用 c++11 编译几乎所有东西。许多代码是多年前用c 编写的。在旧领域开发新课程时,我总是发现自己必须在匹配旧方法或采用更现代的方法之间做出选择。

在大多数情况下,我更喜欢尽可能坚持使用更现代的技术。然而,我经常看到的一个常见的旧做法是位域,我很难争论它的使用。我们传递了很多消息,很多时候,它们都充满了单个位值。以下面的例子为例:

class NewStructure
{
public:

    const bool getValue1() const
    {
        return value1;
    }

    void setValue1(const bool input)
    {
        value1 = input;
    }

private:
    bool value1;
    bool value2;
    bool value3;
    bool value4;
    bool value5;
    bool value6;
    bool value7;
    bool value8;
};

struct OldStructure
{
    const bool getValue1() const
    {
        return value1;
    }

    void setValue1(const bool input)
    {
        value1 = input;
    }

    unsigned char value1 : 1;
    unsigned char value2 : 1;
    unsigned char value3 : 1;
    unsigned …
Run Code Online (Sandbox Code Playgroud)

c++ boolean bit-fields

1
推荐指数
1
解决办法
450
查看次数

标签 统计

bit-fields ×1

boolean ×1

c++ ×1