const& arguments

Dave Morrison (dave@bnl.gov)
Sun, 07 Mar 1999 15:20:07 -0500


Hi all,

This is one that Kyle Pope brought to my attention the other day. I hunted
though the web site and the archives of the mailing list but I didn't see
anything about this. Anyway, it seems that root (2.21/05 at least) is
inconsistent in its handling of `const&' arguments. These are pretty common in
setter methods, so I'd guess that others must have run across this too. To see
what I mean, load the following classes into root:

class a {
public:
void SetA(const Int_t& i) {fA = i;}
void SetX(const Float_t& z) {fX = z;}
Int_t A() {return fA;}
Float_t X() {return fX;}
private:
Int_t fA;
Float_t fX;
};

class b {
public:
void SetA(const Int_t i) {fA = i;}
void SetX(const Float_t z) {fX = z;}
Int_t A() {return fA;}
Float_t X() {return fX;}
private:
Int_t fA;
Float_t fX;
};

They only differ in that class `a' uses const& while class `b' just uses
`const'. Next, run the following macro:

{
a a1;
a1.SetA(1);
a1.SetX(2.0);

b b1;
b1.SetA(1);
b1.SetX(2.0);

printf("a1.A() should be 1 : %d\n", a1.A());
printf("a1.X() should be 2.0: %f\n", a1.X());

printf("b1.A() should be 1 : %d\n", b1.A());
printf("b1.X() should be 2.0: %f\n", b1.X());
}

and you should see

root [0] .L a.h
root [1] .L b.h
root [2] .x test.C
a1.A() should be 1 : 1
a1.X() should be 2.0: 0.000000
b1.A() should be 1 : 1
b1.X() should be 2.0: 2.000000
root [3]

For some reason, `const Float_t&' doesn't seem to work properly while each of
`const Float_t' and `const Int_t' and even `const Int_t&' seems to work OK. Has
anyone else seen this behavior?

Cheers,
Dave

--
David Morrison  Brookhaven National Laboratory  phone: 516-344-5840
                Physics Department, Bldg 510 C    fax: 516-344-3253
		          Upton, NY 11973-5000  email: dave@bnl.gov