const
クラスのインスタンスへの参照を返すメンバー関数があります。
例:
class State
{
const City* city1;
public:
State(const City& c) : city1(c) {}
const City& getReference() const {return *city1;}
void changeStuff();
};
City *
const_castとgetReference()を使用してcity1を指す非constを取得するにはどうすればよいですか?
また、次のことを行うことで、const_castを使用せずに目的を達成することができました:(すでにStateのインスタンスが存在すると仮定してstate1
)
City ref = state1.getReference(); //Why does it work?
City * ptr = &ref; //This is what I wanted, but not this way
ref->changeStuff(); //How can I call functions changing things if the reference was constant?
const参照を返す関数から非const参照を取得し、セッターを呼び出すにはどうすればよいですか?
ご清聴ありがとうございました