Discards qualifiers?

Dear reader,

I have a question

I have (only relevant code shown) a class:

[b]class H1FindPQ: public H1Find {

public:
void Find(TObjArray* particles, TClonesArray* partIds) const;
TH1F GetMkStar0Hist(){ return fMkStar0Hist; }

private:
TH1F fMkStar0Hist;

}[/b]

With in the constructor:

[b]H1FindPQ::H1FindPQ()
{

fMkStar0Hist = TH1F(“MkStar0”,“Inv. mass K^{*0} of all found K#pi combinations”,500,0,5);

}[/b]

And I get from the compiler:

MainPQi386_linux24/…/H1FindPQ.C: In method void H1FindPQ::Find(TObjArray *, TClonesArray *) const': i386_linux24/../H1FindPQ.C:149: passingconst TH1F’ as this' argument ofInt_t TH1::Fill(double)’ discards qualifiers

This I do not understand. Do I not do the very same thing with integers and reals? There I hear no complaints from the compiler? Why is returning the histogram equal to discarting the qualifierl `const’?

Kind regards,

Ytsen de Boer.

Using: gcc version 2.95.3 20010315 (release) , (ROOT 5.15.115, Dec 9 2003)

“uname -r” returns “2.4.26”, I use Linux.

What you do is not legal C++.

You have two solutions;

solution 1

In your constructor, do:
H1FindPQ::H1FindPQ()
:fMkStar0Hist(“MkStar0”,“Inv. mass K^{*0} of all found K#pi combinations”,500,0,5)
{}

solution 2

Instead of:
TH1F fMkStar0Hist;
declare;
TH1F* fMkStar0Hist;
and

 TH1F *GetMkStar0Hist(){ return fMkStar0Hist; }

and
H1FindPQ::H1FindPQ()
{
fMkStar0Hist = new TH1F(“MkStar0”,“Inv. mass K^{*0} of all found K#pi combinations”,500,0,5)
}

  • delete fMkStar0Hist; in the destructor

Rene

Dear Rene,

Thanks for replying, but I do not fully comprehend:

Why is the initialization before the body' of the constructor (your solution 1) acceptable, but the initialization in the constructor itself not? What does this has to do with this apparantly unacceptable (to the compiler) discarting theconst’?

I don’t see the difference with Int_t’s that I initialize also in the constructor and return with { return int; } methods, exactly as I try to do here with the histogram.

Finally: Cannot I just remove the `const’ in the declaration and defenition of H1FindPQ::Find() (solution 3?)?

Regards,

Ytsen.

H1FindPQ::H1FindPQ() { fMkStar0Hist = TH1F("MkStar0","Inv. mass K^{*0} of all found K#pi combinations",500,0,5); } This is legal C++ but is a waste a time. This result in the creation of one empty histo, one full histo and the copy of the later into the former and the deletion of the fomer.

H1FindPQ::H1FindPQ() : fMkStar0Hist("MkStar0","Inv. mass K^{*0} of all found K#pi combinations",500,0,5) { } results in the creation of just one histo.

[quote]MainPQi386_linux24/…/H1FindPQ.C: In method void H1FindPQ::Find(TObjArray *, TClonesArray *) const': i386_linux24/../H1FindPQ.C:149: passingconst TH1F’ as this' argument ofInt_t TH1::Fill(double)’ discards qualifiers
[/quote]The problem is with the implementation of H1FindPQ::Find, you have not provided its content! My guess is that it says something like:

which should be illegal in a const function since it results in the modifiying of the state of your objects (i.e. you break the promise you made by adding the const keyword).

Cheers,
Philippe.

Aha! So if I initialize member data in the constructor body, I am actually wasting time?! Also with integers and reals!?

This happens a lot in official experiment coding!

But I still don’t see why the compiler complains about the line where the member function returns the TH1F. You were right about the filling of this histo in the H1FindPQ::Find() member function, but why does the compiler not complain at that specific line then (the filling)??

Cheers,

Ytsen. :exclamation:

[quote] But I still don’t see why the compiler complains about the line where the member function returns the TH1F. You were right about the filling of this histo in the H1FindPQ::Find() member function, but why does the compiler not complain at that specific line then (the filling)??
[/quote]
I don’t know. Could you send the full content of that function with line numbers?

[quote]Aha! So if I initialize member data in the constructor body, I am actually wasting time?! Also with integers and reals!?
[/quote]There is no waste of time in the case of interger and reals (because they don’t have a constructor). However it is better code style to initialize them in the constructor list.

Cheers,
Philippe.

No, sorry, the compiler complained indeed right at that FIll() command. I mixed the two up (time to go home I think 17.00 hrs -> Automatic brainshutdown :wink: ).

Actually, it is pretty logic now, now I understand.

Thanks!

Ytsen.