Passing object parameter to a functor by reference
I've got a functor that takes a lat3d object as a parameter, and I want to pass this functor to a root finding routine that adjusts ef. The functor looks like:
struct NormalizeNer {
NormalizeNer(lat3d &lat) : lat(lat) {}
double operator()(const double ef) {
lat.setEf(ef);
// some other irrelevant code
}
public:
lat3d lat;
};
The functor is instantiated and passed to the root finding routine findRoot as:
lat3d lattice();
NormalizeNer normalize(lattice);
double efroot = f开发者_运维技巧indRoot(normalize, eflo, efhi, eftolerance);
This works, except that using my class member function lat.setEf(ef) in line 4 only updates lat.Ef in a copy of the lat3d object. I want to be able to pass the lat3d object by reference so I can later get out the last updated value of Ef:
double lastEf = lattice.Ef
Any ideas how to pass by reference here? Apparently using lat3d &lat in line 2 doesn't work. Thanks!
The member variable lat
should be a reference as well:
lat3d ⪫
In your current code, the NormalizeNer
constructor accepts a reference but then makes a copy.
精彩评论