[Home]Lambda Accessing Overloaded Member Functions

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

It is possible to construct a loop which will access overloaded member functions of a class. As this is not in the documentation and I figured out how to make it work in consultation with People/Jaakko Jarvi I am contributing it here. Up until now I thought there was a problem with it which is now resolved. People/John Fletcher.

Consider the following example class.

class A {
    int a;
    double d;
    public :
    A() : a(0), d(0) { }
    A(int aa, double dd = 0) : a(aa), d(dd) { }
   ~A() { }
    void setA(int aa) { a = aa; }
    void setA(double dd) { d = dd; }
    int getA(int aa) const { return a; }
    double getA(double dd) const { return d; }
};

Note that the getA functions have an argument which is only used to force the overloading choice, as the different return type is not sufficient for this.

Now if within some code we have a vector of A objects

    vector<A>   As(10);

and then access the overloaded members setA and getA is possible

    int temp = 0;
    double dtemp = 0;
    for_each(As.begin(),As.end(),(
         var(temp) = bind(static_cast<int (A::* ) (int) const>
                            (&A::getA),_1,var(temp)),
         bind(static_cast<void (A::* ) (int)>
                     (&A::setA),_1,var(temp)), 
         bind(static_cast<void (A::* ) (double)>
                            (&A::setA),_1,var(dtemp))
         )
    );


Lambda?
BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited September 2, 2002 8:40 am (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers