Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = findRoot(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!

like image 762
Tyler Avatar asked Jun 12 '11 19:06

Tyler


1 Answers

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.

like image 134
Fred Foo Avatar answered Oct 17 '22 17:10

Fred Foo