うーむ

            <div class="section">
// Hoge
class Hoge {
private:
bool isHoge;
public:
void SetIsHoge(bool h);
}
void Hoge::SetIsHoge(bool h) {
isHoge = h;
}
// HogeMan
class HogeMan {
private:
std::list<Hoge> hoges;
public:
std::list<Hoge> GetHoges();
}
std::list<Hoge> HogeMan::GetHoges() {
return hoges;
}
// HogeUpdater
class HogeUpdater {
public:
void Update(HogeMan hogeMan);
}
void HogeUpdater::Update(HogeMan hogeMan) {
std::list<Hoge> hoges = hogeMan.GetHoges();
for (std::list<Hoge>::iterator it = hoges.begin();it != hoges.end();it++) {
Hoge* hoge = &*it;
hoge->SetIsHoge(TRUE);
}
}

C++のコードです。

Hoge、HogeMan、HogeUpdater という3つのクラスがある。

Hogehoge の中身。

HogeMan は Hoge をリストで保持している。

HogeUpdater は HogeMan が保持しているリストを元に、Hogeを更新する。

HogeUpdater::Update 関数で HogeMan が保持しているリストをもらい、イテレータでループさせて、Hoge の変数 isHoge をTRUE に更新したいのだが、どうやらこれだと動かないようだ。

おそらく、コピーされた Hoge に対して SetIsHoge(TRUE) を呼んでしまっているのだろうが、どう直したら HogeUpdater から HogeMan が保持しているリスト中の Hoge を更新できるのかわからない。

Help me!

追記

解決した!

コピーを受け渡ししていたので、ポインタか参照を渡すようにする

ポインタ版

// Hoge
class Hoge {
private:
bool isHoge;
public:
void SetIsHoge(bool h);
}
void Hoge::SetIsHoge(bool h) {
isHoge = h;
}
// HogeMan
class HogeMan {
private:
std::list<Hoge> hoges;
public:
/* リストのポインタを返す */
std::list<Hoge>* GetHoges();
}
/* リストのポインタを返す */
std::list<Hoge>* HogeMan::GetHoges() {
return
}
// HogeUpdater
class HogeUpdater {
public:
/* HogeManのポインタを渡す */
void Update(HogeMan* hogeMan);
}
/* HogeManのポインタを渡す */
void HogeUpdater::Update(HogeMan* hogeMan) {
/* HogeMan::hogesのポインタを受け取る */
std::list<Hoge>* hoges = hogeMan.GetHoges();
for (std::list<Hoge>::iterator it = hoges->begin();it != hoges->end();it++) {
Hoge* hoge = &*it;
hoge->SetIsHoge(TRUE);
}
}

//参照版

// Hoge
class Hoge {
private:
bool isHoge;
public:
void SetIsHoge(bool h);
}
void Hoge::SetIsHoge(bool h) {
isHoge = h;
}
// HogeMan
class HogeMan {
private:
std::list<Hoge> hoges;
public:
/* リストの参照を返す */
std::list<Hoge> &GetHoges();
}
/* リストの参照を返す */
std::list<Hoge> &HogeMan::GetHoges() {
return hoges;
}
// HogeUpdater
class HogeUpdater {
public:
/* HogeManの参照を渡す */
void Update(HogeMan &hogeMan);
}
/* HogeManの参照を渡す */
void HogeUpdater::Update(HogeMan &hogeMan) {
/* HogeMan::hogesの参照を受け取る */
std::list<Hoge> &hoges = hogeMan.GetHoges();
for (std::list<Hoge>::iterator it = hoges.begin();it != hoges.end();it++) {
Hoge* hoge = &*it;
hoge->SetIsHoge(TRUE);
}
}