Pointers to functions in arrays
Hey so I'm experimenting with pointers to function and using them to call a desired function based on the number given.
I have tried several things but none of them seem to work.
What I want to do is have an array of pointers of functions defined in a base class (thats a mouth-full) and then call them in the top-level class based on the number I give. So I've made up some general piece of code that demonstrate what I've tried.
hopefully you could help me with figuring this out.
Thanks in advance.
So here was just kind of my first attempt and then I looked online and tried starting from scratch and I just kept getting more confused.
Hopefull this demonstrates what I want to do.
Code:
/*
Testing an array of functions
NOTES
+
*/
#include <iostream>
using namespace std;
class testa{ //this class contains the member function that I want to run
public:
testa();
int a(int x);
int b(int x);
int c(int x);
int d(int x);
private:
int num;
};
testa::testa(){
num=0;
}
int testa::a(int x){
cout<<"a";
return x+1;
}
int testa::b(int x){
cout<<"b";
return x+1;
}
int testa::c(int x){
cout<<"c";
return x+1;
}
int testa::d(int x){
cout<<"d";
return x+1;
}
class testb: public testa{ //At first I tried declaring an instance of class testa but I ran into even more problems so I just made it public
public:
testb();
void run_funcs(int x);
private:
int (*F[4])(int x); //heres the array that I want to fill with functions
};
testb::testb(){
int (*F[4])(int x)={a, b, c, d}; //(this doesnt work either) int (testa::*F[4])(int x)={a, b, c, d};
}
void run_funcs(int x){ //so this should print "abcd" to the screen
for(int i=0; i<4; i++){
//I feel like I should tell it what the included functions are from maybe?
//I tried declaring a testa a called g and then did:
//(g.*F[i])(x);
//and that didnt work either
(*F[i])(x);
}
}
int main(){
int x=0;
run_funcs(x);
//to pause the concole window(im working in an IDE)
char p;
cin>>p;
return 0;
}
The errors I get from microsoft visual c++ express are:
Code:
1>------ Build started: Project: Pointer to function test, Configuration: Debug Win32 ------
1> test.cpp
1>d:\my documents\visual studio 2010\projects\pointer to function test\pointer to function test\test.cpp(48): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'int (__cdecl *)(int)'
1> None of the functions with this name in scope match the target type
1>d:\my documents\visual studio 2010\projects\pointer to function test\pointer to function test\test.cpp(48): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'int (__cdecl *)(int)'
1> None of the functions with this name in scope match the target type
1>d:\my documents\visual studio 2010\projects\pointer to function test\pointer to function test\test.cpp(48): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'int (__cdecl *)(int)'
1> None of the functions with this name in scope match the target type
1>d:\my documents\visual studio 2010\projects\pointer to function test\pointer to function test\test.cpp(48): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'int (__cdecl *)(int)'
1> None of the functions with this name in scope match the target type
1>d:\my documents\visual studio 2010\projects\pointer to function test\pointer to function test\test.cpp(56): error C2065: 'F' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thanks again
Re: Pointers to functions in arrays
Oh boy, what are you really trying to accomplish with this?
Take note that a function pointer is no good for pointing to a class function member.
Code:
class Base
{
public:
int FunctionA(int x)
{
std::cout << "A";
return x + 1;
}
int FunctionB(int x)
{
std::cout << "B";
return x + 1;
}
};
class Derived : public Base
{
public:
Derived()
{
Functions[0] = &Base::FunctionA;
Functions[1] = &Base::FunctionB;
}
void AllFunctions(int x)
{
for(int i = 0; i < 2; ++i)
{
(this->*Functions[i])(x);
}
}
private:
int(Base::*Functions[2])(int);
};
Re: Pointers to functions in arrays
This is extremely helpful.
Just learned about the this pointer.
I'm going to have to do a bit more reading.
Thank you for such a thorough example.
Re: Pointers to functions in arrays
You may find it easier to use a std::function (if your compiler supports it) rather than a function pointer.
Re: Pointers to functions in arrays
hmmm.
std::function?
I cant seem to find an exact definition or use on the internet. Does it have to do with templates?
Or is it just a simple function used in main on the class?
basically: is function, here, a keyword?
And I hope this question isn't too easy.
Re: Pointers to functions in arrays
std::function and std::bind are upcoming features for c++0x.
look at boost's library for info since c++0x versions are based off of boost.
http://en.wikipedia.org/wiki/C%2B%2B...nction_objects
http://www.boost.org/doc/libs/1_46_1...bind/bind.html
simple examples
Code:
class cButton
{
public:
cButton( cWindow& Window );
function< void () > OnClicked;
};
Code:
#include "cGui.h"
using namespace std;
cWindow Window;
cButton Button( Window );
void Button_Clicked()
{
Button.SetName( TEXT( "Clicked" ) );
}
int main()
{
Button.OnClicked = Button_Clicked;
Button.SetName( TEXT( "Click" ) );
Button.SetSize( 125, 25 );
Window.SetInnerSize( 125, 25 );
Window.Show();
return RunApp();
}
Code:
#include "cGui.h"
using namespace std;
class cForm
{
cWindow Window;
cButton Button;
void Button_Clicked()
{
Button.SetName( TEXT( "Clicked" ) );
}
public:
cForm() : Window(), Button( Window )
{
Button.OnClicked = bind( &cForm::Button_Clicked, this );
Button.SetName( TEXT( "Click" ) );
Button.SetSize( 125, 25 );
Window.SetInnerSize( 125, 25 );
Window.Show();
}
};
int main()
{
cForm Form;
return RunApp();
}
Re: Pointers to functions in arrays
If you have a non-ancient version of GCC or MSVC 2010, just include <functional> to get std::function. If you have MSVC 2008, it's called std::tr1::function instead. If you have an even older compiler, you can use boost::function.
Code:
#include <functional>
class MyClass
{
public:
int SomeMemberFunction(int x);
static int SomeStaticMemberFunction(int x);
};
int SomeFreeFunction(int x);
using std::function;
using std::bind;
using std::placeholders::_1;
int main()
{
MyClass c1;
function<int(int)> Func1 = SomeFreeFunction;
function<int(int)> Func2 = MyClass::SomeStaticMemberFunction;
function<int(int)> Func3 = bind(&MyClass::SomeMemberFunction, c1, _1);
Func1(42);
Func2(42);
Func3(42);
}
Re: Pointers to functions in arrays
Whoa!
Okay, thats really interesting. Ya that is probably a more straightforward solution.
Thank you.