passing pointer to member function that is const

This is a discussion on passing pointer to member function that is const within the C++ Programming forums, part of the General Programming Boards category; Hi, my problem is the following let's say we have a class MyClass: class MyClass { int val; bool check(int) ...

  1. #1
    clf
    clf is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Question passing pointer to member function that is const

    Hi,
    my problem is the following
    let's say we have a class MyClass:

    class MyClass {
    int val;
    bool check(int) const;
    };
    bool MyClass::check(int i) const {
    return i==val;
    }


    and a global function f:

    void f( bool (*g)(int) ) {
    cout << (*g)(3) ;


    now what I would like to be able to do is to pass MyClass::check to the function f.

    simply doing

    f(&MyClass::check);

    is not working because the compiler complains it cannot convert 'bool (Myclass::*) (int)' to 'bool (*)(int)'
    and I don't want to make the member function static because it has to access non static attributes. also I don't want my global function to be bound to any specific class (it should only take a function that accept a int and return a bool)

    does anyone know how to solve this problem?

    thanks

    cla

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    class Checkable {
    public:
      bool check(int i) const=0;
    };
    Make the function virtual. Any class that you want to be able to check should implement the Checkable interface. Then, change the signature of f to something like this

    void f(Checkable* g);

    Here, while you can do this with any class you write (just make it support Checkable interface), you can't pass functions to it.

    Alternatively, you could use templates

    Code:
    #include <iostream>
    
    bool retFalseFunc(int i) {
     return false;
    }
    
    class EqualityFunctor {
     int privInt;
    public:
     bool operator()(int i) const {
      return i == privInt;
     }
     EqualityFunctor(int i) : privInt(i) { }
    };
    
    template <typename func> void f(func g) {
     std::cout << "function like thing g returns " << g(2) << " when passed a 2 " << std::endl;
    }
    
    int main() {
     f(retFalseFunc);
     f(EqualityFunctor(3));
     f(EqualityFunctor(2));
     return 0;
    }
    Last edited by SilentStrike; 03-25-2002 at 09:09 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    clf
    clf is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    Thanx A LOT
    i didn't think about the templated solution which is exactly what I needed.
    cla

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21