有一个类定义和一些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发布于 2011-12-31 12:57:27
从评论中:
这里有两个问题
必须在constructor.
const的const,以后无法更改它们。因此,初始化(至少)应该具有常量值的成员。从您以后要更改的成员中删除const。
https://stackoverflow.com/questions/8687106
复制相似问题