Re: [ROOT] HLABEL equivalent in ROOT (fwd)

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Wed Aug 28 2002 - 21:37:26 MEST


Hi Joanne, 

On Wed, 28 Aug 2002 18:35:19 +0200 (MEST)
Joanne Elise Cole <coleje@mail.desy.de> wrote
concerning "Re: [ROOT] HLABEL equivalent in ROOT (fwd)":
> Hi again,
> 
> Thank you to everyone for your suggestions - we have tried them
> in a macro and they work.  However, if we try and do the following in a
> C++ program, it does not work:
> 
> // this array gets filled with the names we want to use to label the axis
> TString names[100];
> 
> // this is the histogram we want to label
> funnel = new TH1D("funnel","funnel",14,0,14);
                                      ^^
                                      |
>                                     |
> // labelling the axis               |
> for (int i=0;i<20;i++){             |
                 ^^^                  |
                   |                  |
                   +------------------+
		   
		   watch out for a SIGSEGV 

>     funnel->GetXaxis()->SetBinLabel(i+1,names[i+30]);
>   }
> 
> when the code is compiled we see the following error:
> 
> passing `const char *' as argument 2 of `TAxis::SetBinLabel(int, char *)'
>  discards qualifiers

Well, the operator that you implcitly use in the line 

     funnel->GetXaxis()->SetBinLabel(i+1,names[i+30]);

is 

   TString::opertator const char* () 

(also known as a conversion operator), and as you can see, it returns
a 

  const char* 

which is _not_ the same as 

  char* 

The standard is very explicit on that point.  The way to do what you
want, is to cast away the const-ness.   That should be done by doing 

     funnel->GetXaxis()->SetBinLabel(i+1, const_cast<char*>(names[i+30]));
  
_Do_not_ do 

     funnel->GetXaxis()->SetBinLabel(i+1, (char*)(names[i+30]));

as the compiler will not use the proper method const_cast<char*>. 

This is the result of most of the newer C++ compilers e.g., GCC 3.x,
actually follow the standard.  Some of newer compiles don't by choice,
like MSVC.NET which have opted for an `industry standard' - which in
plain english means that they will make sure that thier and thier
parterns libraries compile, but that is it.  The situation isn't as
grave as for Fortran77, where each vendor had oh, some 100 extensions
and exceptions. 

Yours, 

> Can anyone tell us what we are doing wrong ?

I hope this works for you. 

Yours, 

 ____ |  Christian Holm Christensen 
  |_| |	 -------------------------------------------------------------
    | |	 Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|	          DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|	          Denmark                    Office: (+45) 353  25 305
 ____|	 Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
 | |



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:06 MET