On 21 Okt., 17:55, ".rhavin grobert" <cl...@yahoo.de> wrote:
> On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.org> wrote:
>
>
>
> > "Jaco Naude" <naude.j...@gmail.com> wrote in message
>
> >news:2fa82026-fb37-4fe6-8227-....
>
> > > I've been struggling with something that should be very simple to
> > > solve... Basically, I get a *const Obj* from a function and I need to
> > > send a pointer to this object to a function accepting only a Obj*.
>
> > No you don't. *Or at least you don't need to ask advice about how to do it.
>
> > If you have a const Obj*, that means you have a pointer to an object that
> > you have promised not to modify.
>
> > If you have a function that accepts only an Obj*, it means that the function
> > reserves the right to modify the object.
>
> > So what you are trying to do is to figure out how to break your promise not
> > to modify the object. *The best advice -- and the only advice I can advocate
> > until you convince me that it is really important to do otherwise -- is
> > "Don't do it."
>
> Guess the following:
>
> int Obj::Read() const;
> int Obj::Write(int iWhatever);
>
> int PrettyFunction(int iCommand, Obj* pObject, int iAux)
> {
> * switch(iCommand)
> * {
> * case CMD_READ:
> * * return pObject->Read();
> * case CMD_WRITE:
> * * return pObject->Write(iAux);
> * default:
> * * DropBombOnWhiteHouse();
> * }
>
> }
>
> If you have a Obj const* it would still be apropriate to call the
> PrettyFunction with the CMD_WRITE by const_cast'ing.
err, i meant "CMD_READ" and forgot a "return"