Re: [ROOT] Cint warnings on const members

From: ZVYAGIN (ZVYAGIN@mx.ihep.su)
Date: Sat Feb 03 2001 - 08:21:09 MET


Hello,

On Fri, 2 Feb 2001, Rene Brun wrote:

> Hi Victor,
> 
> In a class member function declared const, you can call a non-const
> function of the class by a dirty cast. See example below.
> 
> CBananaPlant::Function() const {
>  ((CBananaPlant *)this)->SomeNonConstFunction();
> }

It is much better for this dirty trick to use C++ keyword const_cast:

 CBananaPlant::Function() const {
  const_cast<CBananaPlant*>(this)->SomeNonConstFunction();
 }

That is C++ style of removing constness.

With best wishes,
Alexander.

> 
> This is a dirty solution, clearly violating the intended security with const.
> You better solve the problem with const by making sure that all the class
> functions called from a const fonction are also const.
> Sometimes, this is useful.
> 
> Rene Brun
> 
> 
> Victor Krylov wrote:
> > 
> > Hello everybody,
> > 
> > I have a class with some const members.
> > Somebody knows how to deliver from the warnings?
> > 
> > BananaPlantCint.cxx: In method `void CBananaEvent::Streamer(class
> > TBuffer &)':
> > BananaPlantCint.cxx:258: warning: converting to `CBananaPlant *' from
> > `const CBananaPlant *' discards const
> > BananaPlantCint.cxx:258: warning: initializing non-const `CBananaPlant
> > *&' with `const CBananaPlant *' will use a temporary
> > BananaPlantCint.cxx:40: warning: in passing argument 2 of `operator
> > >>(TBuffer &, CBananaPlant *&)'
> > 
> > Regards,
> >         Viktor
> 



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:35 MET