Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CA
- {
- public:
- CA(void) {};
- ~CA(void) {};
- void setA(double x) {a = x; };
- void setB(double x) {b = x; };
- double getA(const double x) {return x*a; };
- double getB(const double x) {return x*b; };
- void print(double f(const double), double x) {
- char cTemp[256];
- sprintf_s(cTemp, "Value = %f", f(x));
- std::cout << cTemp;
- };
- private:
- double a, b;
- };
- CA cA;
- cA.setA(1.0);
- cA.setB(2.0);
- double (*p)(const double);
- if(true) {
- p = &cA.getA; //'&' : illegal operation on bound member function expression
- } else {
- p = cA.getB; //'CA::getB': function call missing argument list; use '&CA::getB' to create a pointer to member
- //'=' : cannot convert from 'double (__thiscall CA::* )(const double)' to 'double (__cdecl *)(const double)'
- }
- cA.print(p, 3.0);
- // OK for global functions
- double (*p)(const double);
- // OK for member functions
- double (CA:*p)(const double);
- p = &CA::getA;
- CA cA;
- (cA.*p)();
- #include <iostream>
- void print(double (CA::*f)(const double), double x)
- {
- // Rather use the C++ I/O Library if you can...
- std::cout << "Value = " << (this->*f)(x);
- };
- int main()
- {
- CA cA;
- cA.setA(1.0);
- cA.setB(2.0);
- double (CA::*p)(const double);
- if (true) // Maybe use some more exciting condition :-)
- {
- p = &CA::getA;
- }
- else {
- p = &CA::getB;
- }
- cA.print(p, 3.0);
- }
- #define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember))
- class CA
- {
- public:
- typedef double (CA::*CAGetter)(const double x);
- void print(CAGetter f, double x)
- {
- std::cout << "value = " << CALL_MEMBER_FN(*this, f)(x) << 'n';
- }
- CA a;
- a.setA(3.1);
- a.setB(4.2);
- // Using a variable...
- CA::CAGetter p = &CA::getA;
- a.print(p, 1);
- // without a variable
- a.print(&CA::getB, 1);
- // Calling the functions from outside the class...
- std::cout << "From outside (A): " << CALL_MEMBER_FN(a, p)(10) << std::endl;
- std::cout << "From outside (B): " << CALL_MEMBER_FN(a, &CA::getB)(10) << std::endl;
- class CA
- {
- public:
- typedef double (CA::*getter)( double );
- CA(void) {};
- ~CA(void) {};
- void setA(double x) {a = x; };
- void setB(double x) {b = x; };
- double getA(const double x) {return x*a; };
- double getB(const double x) {return x*b; };
- void print(getter f, double x) {
- char cTemp[256];
- sprintf(cTemp, "Value = %f", (this->*f)(x));
- std::cout << cTemp;
- };
- private:
- double a, b;
- };
- int main()
- {
- CA cA;
- cA.setA(1.0);
- cA.setB(2.0);
- CA::getter p;
- if(true) {
- p = &CA::getA;
- } else {
- p = &CA::getB;
- cA.print( p, 3.0 );
- }
- class CA
- {
- public:
- typedef boost::function<double( double )> getter;
- CA(void) {};
- ~CA(void) {};
- void setA(double x) {a = x; };
- void setB(double x) {b = x; };
- double getA(const double x) {return x*a; };
- double getB(const double x) {return x*b; };
- void print(getter f, double x) {
- char cTemp[256];
- sprintf(cTemp, "Value = %f", f(x));
- std::cout << cTemp;
- };
- private:
- double a, b;
- };
- int main()
- {
- CA cA;
- cA.setA(1.0);
- cA.setB(2.0);
- CA::getter p;
- if(true) {
- p = boost::bind( &CA::getA, &cA, _1 );
- } else {
- p = boost::bind( &CA::getB, &cA, _1 );
- }
- cA.print( p, 3.0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement