Jump to content

How to call a function pointer?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hello
I have this code:
	int (*state_machine[MAX_EVENTS][MAX_STATES]) (StateMachine *ac);
and it is already initialized to contain different functions and everything. I just am not sure how I can use it?

It is defined inside a class called StateMachine and so in my own class I have this definition:

StateMachine statemachine;

When I write:
statemachine->state_machine[1][1];

my compiler says: "Statement has no effect". What does it mean?

Basically it is supposed to be the implementation of a statemachine that I'm trying to use in my own class. The state machine has already been given to me.
Any ideas?

Thanks alot

#2
DRK

DRK

    Newbie

  • Members
  • PipPip
  • 10 posts
You have to use () to call a function, without it's just a function address.
statemachine->state_machine[1][1]();


#3
Edward

Edward

    Newbie

  • Members
  • Pip
  • 7 posts
Are you trying to call a method of StateMachine using function pointer?

#4
kernelcoder

kernelcoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 282 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal
I don't know why your code(function pointer) is taking StateMachine as argument. Perhaps an instance of StateMachine is going as an argument to method of another StateMachine instance, right?

Also, you are accessing a class variable (the array "state_machine") from outside of class variable which is not good. You should declare the array as private/protected for that class and use member function to access it.

Anyway, following is the example code which shows how to declare, assign and access member-function pointer.

#define MAX_EVENTS 5
#define MAX_STATES 5
#define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))

class StateMachine; // froward declaration

typedef int (StateMachine::*state_machine) (StateMachine *arg1, int arg2, int arg3);

class StateMachine
{
	public:
	StateMachine()
	{
// initialize function pointers array here
stateMachines[0][0] = &StateMachine::method1;
stateMachines[0][1] = &StateMachine::method2;
// remainings will go here...

	}

	state_machine stateMachines[MAX_EVENTS][MAX_STATES];


	int callFunctionPointer(int row, int col, StateMachine* arg1, int arg2, int arg3)
	{
return (this->*(stateMachines[0][0]))(arg1, arg2, arg3);
	}

	private:
	int method1(StateMachine* sm, int arg2, int arg3)
	{
// use sm here
return 0;
	}

	int method2(StateMachine* sm, int arg2, int arg3)
	{
// use sm here
return 0;
	}
};


int main()
{
	StateMachine sm1;
	StateMachine sm2;
	int returnValue = CALL_MEMBER_FN(sm1, sm1.stateMachines[0][0])(&sm2, 1, 2); // this is good as it is readable to the readers of the code
	returnValue = (sm1.*(sm2.stateMachines[0][0]))(&sm2, 1, 2); // this is bad
	returnValue = sm1.callFunctionPointer(0,0, &sm2, 1, 2); // this is good too; anyone who  has allergy with macro, can use this way.
	return 0;
}

Hope this help you!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users