How do I call a method pointer?

Ok I appear to have done my head in.

I have a class called codon:

1
2
3
4
5
6
7
8
typedef double const (Codon::*t_p_codon_fun)(Ribosome * const) const;
class Codon
{
t_p_codon_fun Fun(); // returns a pointer to a codon function

// here is a list of codon functions that look like this:
double const ABC(Ribosome * const) const; // hundreds of these
};


So now, somewhere else, I want to run the function that Codon::Fun() returns from a particular instance. How do I do this? I have an iterator to an instance of Codon:

1
2
auto pointer = &(*it_codon_sequence); // turn the iterator into a pointer (!?)
(pointer->*pointer->Fun())(this); // !? 


This is the only way I have been able to get it to work. But does this make sense? Is there an easier/prettier way? I find this whole method-pointer business quite confusing...
closed account (zb0S216C)
You can call the function this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Simple
{
  public:
    void Function( ) const;
};

int main( )
{
  Simple Instance;
  void( Simple:: *FunctionPointer )( ) const;
  
  // Call the function through the pointer:
  ( Instance.*FunctionPointer )( );
}

Wazzak
If I do this:

1
2
auto pointer = &(*it_codon_sequence);
(pointer->*Fun())(this);


I get an error saying:

error C3861: 'Fun': identifier not found


But if I do this:

1
2
auto pointer = &(*it_codon_sequence);
(pointer->*pointer->Fun())(this);


then it works fine. Why is that?

EDIT: The above code is being run from within a different class' method, if that makes a difference. I am using VS2012.
Last edited on
Topic archived. No new replies allowed.