class RO
method を持つ親がいvoid setup(const int* p)
ます。class RW
非定数ポインターのみを許可する同じメソッドを持つ子が必要です。
で 2 つのメソッドを作成しclass RO
、 でそれらの 1 つを許可しないことでそれを行いclass RW
ます。
class RO
{
public:
void setup(int* p) { DO SMTH }
virtual void setup (const int* p) { RO::setup( const_cast<int*>(p) ); }
// the rest...
void read() const;
};
class RW : public RO
{
public:
virtual void setup (const int* p) { throw SMTH }
// the rest...
void write();
};
RW::setup
可能であれば、コンパイル時に許可しないようにしたいと思います。すなわち、
const int* p;
RO* ro = new RW;
ro->setup(p); // Throw at run time, since it can be unknown
// at compile time that ro points to RW and not to RO.
RW* rw = new RW;
rw->f(p); // Disallow this at compile time, since it is
// known at compile time that rw points to RW.
それを行う方法はありますか?