1

Possible Duplicate:
C++ member-function pointer
How to invoke pointer to member function when it's a class data member?

I've only recently started using C++, so I apologize if the following contains any trivial mistakes, or if I missed an easier solution. I would like to achieve something like this:

class ClassA {

typedef double (ClassA::*CondFunc)();
 public:
    ClassA(int x, int y) {

        value_ = x;

        switch (y) {
            case 0:
                condFunc_ = &ClassA::condA;
                break;
            case 1:
                condFunc_ = &ClassA::condB;
            default:
                break;
        }
    }

    ~ClassA();

    int value_;
    CondFunc condFunc_;
    double condA() { return 2.0*value_; }
    double condB() { return 4.0*value_; }

    void Test() {
        int a = condFunc_(); // compile error
    } 
};

but get a compile error in Test(). Please note that this is a vastly simplified function and is not supposed to make any sense. I've searched this forum and elsewhere for answers, but am still not sure whether defining/calling such non-static member function pointers is even possible. The only plausible hint/solution I've come across employs a static wrapper function to achieve something similar. I'd be grateful for any help/clarifications.

Community
  • 1
  • 1
f4_
  • 23
  • 4
  • 2
    It's terribly useful in the case of compiler errors to list the actual error message you're receiving. – Mark Ransom Mar 12 '12 at 03:55
  • My immediate reaction is that we could probably help more if you would take a step back (so to speak) and tell us a bit more about what you're trying to accomplish. In roughly 25 years of using C++, I could probably count all the times I've used pointers to member functions on my fingers. – Jerry Coffin Mar 12 '12 at 03:56
  • The compiler error I get is (with Xcode): Called object type 'CondFunc' (aka 'double (ClassA::*)()') is not a function or function pointer. As for the reason I'm trying to do this: partial functionality of the member function must depend on 'y'; in the actual function this is called within a loop, and I wanted to avoid a switch. – f4_ Mar 12 '12 at 04:08
  • @iammilind No, since the assignment happened within the member function, and not on an instantiated object – f4_ Mar 12 '12 at 04:12
  • @JerryCoffin Seeing that the solution wasn't easy to find, I suspected that I may not be doing this in the most straightforward fashion. What would your suggestion be to achieve the same functionality/code structure? – f4_ Mar 12 '12 at 04:47
  • @f4_: I'm not sure exactly what you're trying to accomplish. One possibility might be a pure virtual function in the base class, with two derived classes, one that overrides the virtual with `condA` and the other with `condB` -- but without knowing what you're really trying to accomplish, it's hard to guess whether that's an improvement. – Jerry Coffin Mar 12 '12 at 04:56

1 Answers1

4

You have to call the member pointer function like this:

    int a = (this->*condFunc_)(); 
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132