working with std::bitset



有一个类定义和一些bool函数来测试一些属性

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

问题是如何在调用needsResponse()之前将NeedsResponse设置为true或false

请注意attributesstd::bitset类型

更新:

我写了这个函数:

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

但是我得到这个错误:

error: lvalue required as left operand of assignment

从注释中:

这里有两个问题

    const类型的数据成员必须在类的构造函数中初始化。
  1. 如果成员为const,则无法更改。

因此,初始化(至少)应该具有常数值的成员。从以后要更改的成员中删除const

最新更新