Function Pointers from external class

How can i use a function pointer from external class? Say we have a class tuna

Tuna:

1
2
3
4
5
6
7
8
9
10
int foo(){

return 45;
}

int (*foobar)();

Tuna::Tuna(){
   foobar = foo; // this is an error.. gives me an incompatible type
}


This doesnt work;

while on main it works

1
2
3
4
5
int main(){

foobar = foo; // this works

}

http://ideone.com/8Aqyry

Works perfectly fine. I suspect you're actually using code that differs from what you posted.
No. the Tuna is not a struct but an external class.

foo and foobar is declared in header of the Tuna class. Like this

1
2
3
4
5
6
7
8
9
Tuna::Tuna()
{
 foobar = foo;

}

int Tuna ::foo(){
	return 45;
}


and this is the header

1
2
3
4
5
6
7
8
9
class Tuna
{
public:
	Tuna();

	int foo();
	int(*foobar)();

};



and this is the error i got

IntelliSense: a value of type "int (Tuna::*)()" cannot be assigned to an entity of type "int (*)()"
Last edited on
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
#include <iostream>

struct tuna
{
    tuna( int i ) { foobar = &tuna::foo ; baz = &tuna::bar ;  v = i ; }
    
    int foo() const
    { 
        std::cout << "tuna::foo - callewd on object at address " << this << '\n' ;
        return v + 45 ;
    }
    
    int bar( int i )
    { 
        std::cout << "tuna::bar(" << i << ") - callewd on object at address " << this << '\n' ;
        v += i ;
        return v + 45 ;
    }

    int (tuna::*foobar) () const ; // pointer to member function (const)
    int (tuna::*baz) (int) ; // pointer to member function (non-const) taking an int
    
    int v ;
};

int main()
{
    int (tuna::*ptr_mem_fun)(int) = &tuna::bar ;
    
    tuna object(23), another_object(75) ;
    tuna* pointer = &another_object ;
    
    (object.*object.foobar)() ;    
    (object.*object.baz)(50) ;
    (object.*ptr_mem_fun)(60) ;
    std::cout << '\n' ;

    (another_object.*object.foobar)() ;
    (another_object.*ptr_mem_fun)(70) ;
    std::cout << '\n' ;

    (pointer->*pointer->foobar)() ;    
    (pointer->*pointer->baz)(80) ;    
    (pointer->*ptr_mem_fun)(90) ;
}

http://coliru.stacked-crooked.com/a/a7faed5e905e4583

Consider using std::function<> instead.
http://en.cppreference.com/w/cpp/utility/functional/function
procrastination, as you can see from the error message, the type of Tuna::foo and the type of Tuna::foobar don't match up.

You should keep in mind that a member method of a class has a hidden this parameter in the parameter list so that the signature for Tuna::foo is actually something like: int foo(Tuna* this). As you can see that differs from a function which takes no arguments and returns an int. If you want to use a regular member function, you should see JLBorge's code above.

Alternately, you could make the member function static. The one you've supplied doesn't depend on the object in any way so there isn't a reason for it to be non-static. Of course, that may be because you simplified it for the posting.

The other alternative, as mentioned by JLBorges, is to use a std::function object:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <functional>

int foo()
{
    std::cout << "Invoked ::foo\n";
    return 99;
}

struct Functor
{
    Functor(int val) : _value(val) {}
    int operator()() 
    {
        std::cout << "Invoked Functor::operator()\n";
        return _value; 
    }

private:
    int _value;
};

struct Class
{
    Class(int x) : _value(x*x) {}
    int foo() const 
    { 
        std::cout << "Invoked Class::foo with this = " << this << '\n';
        return _value; 
    }

private:
    int _value;
};


struct Func
{
    using function_type = std::function<int()>;

    Func(function_type f) : _func(f) {}
    
    static int foo() 
    {
        std::cout << "Invoked Func::foo\n";
        return 46;
    }

    int invoke() const { return _func(); }

private:
    function_type _func;
};

int main()
{
    Class c(7);

    auto lambda = [] { std::cout << "Invoked lambda\n"; return 32; };

    std::vector<Func> v =
    {
        { foo },
        { Functor(69) },
        { std::bind(&Class::foo, &c) },  // http://en.cppreference.com/w/cpp/utility/functional/bind
        { Func::foo },
        { lambda }
    };

    for (auto & f : v)
        std::cout << f.invoke() << "\n\n";
}


http://ideone.com/gEdsKq




Topic archived. No new replies allowed.