私はC++を学んでいて、このconst_cast演算子に出くわしました。次の例を考えてみましょう。
class Test
{
private:
char name[100];
public:
Test(const char* n) { std::strncpy(name, n, 99); name[99]=0; }
const char* getName() const { return name; }
}
これで、ユーザーは次のことができます
Test t("hi");
const_cast<char*>(t.getName())[0] = 'z'; //modifies private data...
これでいいですか?return const char *の目的はプライベートデータの変更を防ぐことだったので、プライベートデータを変更することを意味します。これを防ぐにはどうすればよいですか?(std :: stringを使用せずに)