使用std :: bitset

时间:2011-12-31 08:08:46

标签: c++ 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

请注意,attributes的类型为std::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

1 个答案:

答案 0 :(得分:1)

来自评论:

这里有两个问题

  1. 必须在类构造函数中初始化const的数据成员。
  2. 如果成员为const,则以后无法更改。
  3. 因此,初始化(至少)应该具有常量值的成员。从您稍后要更改的成员中删除const

相关问题