2011-12-31 9 views
0

クラス定義とSTDでの作業::ビットセット

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

質問は、私がtrueまたはfalseにNeedsResponseを設定することができる方法である前

needsResponse()を呼び出すにはいくつかの属性をテストするいくつかのブール関数がありますされますattributesのタイプがあることに注意してstd::bitset

UPDATE:

私はこの機能を書いた:

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 
+0

あなたは 'bitset'の様々なビットの状態を取得するメソッドを持っています。なぜ、さまざまなビットの状態を設定するためのメソッドを追加しないでください。あるいは私は質問のポイントを逃している? –

+0

あなたは正しいです。どうやってやるの?スニペットを教えていただけますか? – mahmood

+0

この宿題はありますか?もしそうなら、私は彼らがおそらく 'testCmdAttrib()'のように見えるだろうというヒントを与えます。 –

答えて

1

のコメント:

ここでは二つの問題

  1. データメンバーがあります。つまり、constはinitializでなければなりませんクラスコンストラクタで編集します。
  2. メンバーは後で変更する方法はありませんconstしている場合。

したがって、定数値を持つと思われるメンバーを(少なくとも)初期化します。後で変更するメンバーからconstを削除します。