Pointers to functions are hard to grasp, what is wrong here?

Hi guys,

I am having troubles with pointers to functions. I am doing something wrong and I just don't see it.
I define a pointer to a member of a class, the pointer is called SelectedCondition, which points to one of 3 class methods, depending on the value of a flag.

The error I get is:

In member function 'bool IntegerCondition::Examine(int)':
../tools//eventselection.h:50: error: invalid use of 'unary *' on pointer to member

take a look at the code, it's fairly simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

class IntegerCondition{
private:

  int test;
  string rel;


public:

  //Constructor
  IntegerCondition(){
    test=-1;
    rel="norelation";
  }
 
  //This is the pointer to the member function.
  bool  (IntegerCondition::*SelectedCondition)(int);

  //These are the methods to which SelectedCondition should point.
  bool IsItAbove(int num){
    return num > test;
  }
  bool IsItEqual(int num){
    return num == test;
  }
  bool IsItBelow(int num){
    return num < test;
  }
 //Now I selected which method exactly is pointed to by SelectedCondition.
 void Set(int testinteger,string relation){
    test=testinteger;
    rel=relation;

    if(rel=="above"){
      SelectedCondition=&IntegerCondition::IsItAbove;
    }else if(rel=="equal"){
      SelectedCondition=&IntegerCondition::IsItEqual;
    }else if(rel=="below"){
      SelectedCondition=&IntegerCondition::IsItBelow;
    }else{
      SelectedCondition=NULL;
    }

  }

  //This is the line that causes trouble.
  //I simply want to evaluate the function to which SelectedCondition points to.
  bool Examine(int num){
    return (*SelectedCondition)(num);
  }

}


Any ideas? Thanks a lot!
Last edited on
You need to an instance of the object when you call (non-static) member functions.
Try replacing line 50 with return (*this.*SelectedCondition)(num);
Topic archived. No new replies allowed.