Passing member function to member fct

Hey, I want to pass a non static member function to another member of the same class.
Following basic example:

//---------------------------------
//functionclass.h
1
2
3
4
5
6
7
8
Class FunctionClass
{
public:
typedef  void (FunctionClass::*funcPtr)(double,double*,double*,double*);
void innerfcn1(double a, double* b, double* c, double* d);
void innerfcn2(double a, double* b, double* c, double* d);
void outerfcn(funcPtr passingfunctionpointer,double e, double* f,double* g, double* h);
}

//---------------------------------
//functionclass.cpp
1
2
3
4
5
6
7
8
9
#include functionclass.h
...

outerfcn(funcPtr passingfunctionpointer,double e, double* f,double* g, double* h){
...
//This should call the passed function (either innerfcn1 or innerfcn2)
(this->*passingfunctionpointer)(e,f,g,e); 
...
}

//---------------------------------
//main.cpp
1
2
3
4
5
6
#include functionclass.h
int main(){
...
FunctionClass::outerfcn(FunctionClass::innerfcn1,i,j,k,l);
...
}


I used the method of helios and no more compiler issues.
Unfortunately now I have the problem if I initialize a matrix with double** matrix= ...malloc....
and I call a function with (this->*passingfunctionpointer)(e,f,g,e);,
than the memory of the matrix is affected eventhough, it's not manipulated within the function or even given to the function. Following code sequence.

init the matrix with zeros -> Memory shows just zeros
Call of the function with the function pointer (this->*passingfunctionpointer)(e,f,g,e):
The passed variables don't have any relation with the matrix or its memory.
ALso the matrix isn't called within the function.
After the function call:
The matrix is pointing to crazy places in some rows.
No idea what the heck is going on.
Can you help me?
Thanks in advance
Last edited on
1
2
3
class Foo{
    //Declare pointer-to-member typedefs here.
};
Sorry, I don't get this reference.
Could you explain your approach a little bit more detailed?
Does that mean to declare the typedef within the FunctionClass?

Yes.
I changed the code accordingly. Now I get following errors?
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘innerfcn1 (...)’, e.g. ‘(... ->* innerfcn1) (...)’
Last edited on
As the error says. (this->*pointer)(parameter)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

struct A
{
    template < typename PTR_MEM_FUN, typename... ARGS >
    auto outer_function( PTR_MEM_FUN ptr_mem_fun, ARGS... args ) // simple pass by value
    { return (this->*ptr_mem_fun)( args... ) ; }

    void foo( int a, double b ) { std::cout << "A::foo(" << a << ',' << b << ")\n" ; v += a+b ;  }
    int bar( const char* msg ) const { std::cout << "A::bar('" << msg << "')\n" ; return v ;  }
    double baz() const { std::cout << "A::baz()\n" ; return v * 1.2 ;  }

    int v = 0 ;
};

int main()
{
    A a ;
    a.outer_function( &A::foo, 1, 2.3 ) ;
    a.outer_function( &A::bar, "hello" ) ;
    a.outer_function( &A::baz ) ;
}

http://coliru.stacked-crooked.com/a/75f1c46c13bc63e5
Topic archived. No new replies allowed.