Returning Pointer-to-Member from a Class Method

Gentlemen,

I need to work with pointer-to-functions, specially pointer-to-members of a class, and I got myself stucked when trying to return the pointer through a method. Please, take a time to view the code:

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
#ifndef CFUNC_H_
#define CFUNC_H_

#include <string>
#include <vector>

class CFunc{
private:
    struct function_table {
       string symbol;
       size_t arity;
       double (CFunc::*op_func)(double*);
    };    
    vector<function_table> fx;
    double add_(double *a){return a[0]+a[1];}
    double sub_(double *a){return a[0]-a[1];}
    double prod_(double *a){return a[0]*a[1];}
    double div_(double *a){return (a[1]) ? a[0]/a[1] : 1.0;}
public:
    CFunc(){
        function_table Fx;
        Fx.symbol = "+"; Fx.arity = 2; Fx.op_func = &CFunc::add_;
        fx.push_back(Fx);
        Fx.symbol = "-"; Fx.arity = 2; Fx.op_func = &CFunc::sub_;
        fx.push_back(Fx);
        Fx.symbol = "*"; Fx.arity = 2; Fx.op_func = &CFunc::prod_;
        fx.push_back(Fx);
        Fx.symbol = "%"; Fx.arity = 2; Fx.op_func = &CFunc::div_;
        fx.push_back(Fx);
    }
    size_t getFuncSize(){ return fx.size(); }
    string getFuncSymbolAt(size_t p){ return fx.at(p).symbol; }
    size_t getFuncArityAt(size_t p){ return fx.at(p).arity; }
    double (CFunc::*)(double*) getFuncFunctionAt(size_t p){// <== First error
        return fx.at(p).op_func(double *a); // <== Second error
    } 
    virtual ~CFunc();
};

#endif /* CFUNC_H_ */ 


The errors I'm having are:

1
2
3
4
\CFunc.h:34 expected unqualified-id before ')' token 
\CFunc.h:34 expected ';' at end of member declaration 
\CFunc.h:34 ISO C++ forbids declaration of 'getFuncFunctionAt' with no type 
[-fpermissive] 


and

1
2
3
4
5
6
7
8
9
\CFunc.h:35 must use '.*' or '->*' to call pointer-to-member function in 
'((CFunc*)this)->CFunc::fx.std::vector<_Tp, _Alloc>::at [with _Tp = 
CFunc::function_table, _Alloc = std::allocator<CFunc::function_table>, 
std::vector<_Tp, _Alloc>::reference = CFunc::function_table&, std::vector<_Tp, 
_Alloc>::size_type = unsigned int](p).CFunc::function_table::op_func (...)', 
e.g. '(... ->* ((CFunc*)this)->CFunc::fx.std::vector<_Tp, _Alloc>::at [with _Tp 
= CFunc::function_table, _Alloc = std::allocator<CFunc::function_table>, 
std::vector<_Tp, _Alloc>::reference = CFunc::function_table&, std::vector<_Tp, 
_Alloc>::size_type = unsigned int](p).CFunc::function_table::op_func) (...)' 


I have no idea of how to solve those. Has anyone ever faced this problem? How to handle pointers-to-class-members?

Thank you for your attention.
Try changing
1
2
3
4
5
    string getFuncSymbolAt(size_t p){ return fx.at(p).symbol; }
    size_t getFuncArityAt(size_t p){ return fx.at(p).arity; }
    double (CFunc::*)(double*) getFuncFunctionAt(size_t p){
        return fx.at(p).op_func(double *a);
    }

to
1
2
3
4
5
    string getFuncSymbolAt(size_t p){ return fx.at(p)->symbol; }
    size_t getFuncArityAt(size_t p){ return fx.at(p)->arity; }
    double (CFunc::*)(double*) getFuncFunctionAt(size_t p){
        return fx.at(p)->op_func(double *a);
    }


See what happens.
Nope :-(

The first error is still there.

A new error on lines 32, 33 and 35:
 
base operand of '->' has non-pointer type 'CFunc::function_table' 


and line 34 has an error:
 
\CFunc.h:34 expected primary-expression before 'double' 


I'm changing it back for now...
Topic archived. No new replies allowed.