简体   繁体   English

使用std :: bitset

[英]working with std::bitset

There is a class definition and some bool functions which test some attributes 有一个类定义和一些用于测试某些属性的布尔函数

  class MemCmd
  {
      friend class Packet;
      public:
        enum Command
        {
          InvalidCmd,
          ReadReq,
          ReadResp,
          NUM_MEM_CMDS
        };
      private:
        enum Attribute
        {
          IsRead,         
          IsWrite,             
          NeedsResponse,  
          NUM_COMMAND_ATTRIBUTES
        };

        struct CommandInfo
        {
          const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
          const Command response;
          const std::string str;
        };
        static const CommandInfo commandInfo[];
      private:
        bool
        testCmdAttrib(MemCmd::Attribute attrib) const
        {
          return commandInfo[cmd].attributes[attrib] != 0;
        }
      public:
        bool isRead() const         { return testCmdAttrib(IsRead); }
        bool isWrite() const        { return testCmdAttrib(IsWrite); }
        bool needsResponse() const  { return testCmdAttrib(NeedsResponse); }
  };

The question is how can I set NeedsResponse to true or false prior to calling needsResponse() 现在的问题是如何设置NeedsResponse为true或false调用之前needsResponse()

Please note that attributes is of type std::bitset 请注意, attributes的类型为std::bitset

UPDATE: 更新:

I wrote this function: 我写了这个函数:

void
setCmdAttrib(MemCmd::Attribute attrib, bool flag) 
{
    commandInfo[cmd].attributes[attrib] = flag;   // ERROR
}

void setNeedsResponse(bool flag)   { setCmdAttrib(NeedsResponse, flag); }

But I get this error: 但是我得到这个错误:

error: lvalue required as left operand of assignment

From the comments: 从评论:

There are two problems here 这里有两个问题

  1. Data members that are const must be initialized in the class constructor. const数据成员必须在类构造const中初始化。
  2. If the members are const there is no way to change them later. 如果成员是const ,则以后无法更改它们。

So, initialize (at least) the members that are supposed to have a constant value. 因此,初始化(至少)应该具有恒定值的成员。 Remove const from the members you intend to change later. 从您打算以后更改的成员中删除const

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

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