Advertisement

Function Pointers

Started by May 27, 2000 09:01 PM
7 comments, last by Possibility 24 years, 7 months ago
This is a fairly technical question so I hope someone can help me. In my C++ book, i read about function pointers, here is how they showed how to use it: void Fuction() { } void main() { void (*function_pt)() = Function; //sets the pointer (*function_pt)(); //this then executes Function() } thats just the basic system, but here is what I wanted to, kind of like this: void Function() { } class TEST { TEST(); void (*function_pt)(); void Test_Function(); }test; TEST::TEST() { (*function_pt)() = Function; //set the pointer here } void TEST::Test_Function() { (*function_pt)(); //execute Function() here } void main() { test.Test_Function(); //call to Test_Function so that Function() gets executed } But this doesnt work, it gives me weird errors. Can anyone help me do something like this? Possibility
when you''re assigning a function pointer, it''s just like any other variable.

function_pt = Function;

and to call the function to which it points, you DON''T need to dereference it, because it is implicit. Watch:

(*function_pt)();

is redudant and should be written like this:

function_pt();

With that being said, I think your problem lies in the constructor. Fix the assignment and it should work.
Advertisement
i think this will correct the problem:

TEST::TEST()
{
function_pt = Function; //set the pointer here
}

when you''re declaring function_pt, it''s ok to initialize it like
void (*function_pt)() = Function;
but when you''ve already defined the pointer, you have to set it up like that.

this is exactly like this:
int i;
int *j = &i
this one works cuz u''re doing it at initialization, but if you say:
int i;
int *j;
*j = &i //note that this one will cause an error
so you have to say:
int i;
int *j;
j = &i //you can''t put the * here anymore cuz u''ve already defined the pointer

i hope this helped you

- pouya
--------------
When I die, I want to go like my grandfather did, peacefully in his sleep. Not yelling and screaming like all the passengers in his car.
ya, foofighter finished his message earlier than mine

- pouya
--------------
When I die, I want to go like my grandfather did, peacefully in his sleep. Not yelling and screaming like all the passengers in his car.
Ah sweet! that worked perfectly pouya. Thanks alot guys.

Possibility
Ah crap, it doesnt work for pointing to a function with in a class, like:

TEST::TEST()
{
function_pt = class_A.Function; //set the pointer here
}

this gives an error, and I tried putting * and & in every which possible way but all give basically the same error.

Can this be done?

Possibility

Advertisement
Ok, here is the code I am using, this is a test
respresentation of what the code in my game will
be using, the actual code is far far more complex
with many more then just 3 classes


#include #include #include #include #include class A{public:	void Test_Function();} class_A;void A::Test_Function(){        cout<<"Made it to the class_A.Test_Function";}class B{public:	void Test_Function();} class_B;void B::Test_Function(){        cout<<"Made it to the class_B.Test_Function";}class TEST{public:	TEST();	void (A::*function_pt)();	//the problem is here, I have to specifically	//declare this function to class A, but i dont want that, i want	//it to be completely generic, so that it can also point to class B	void function();} test[2];TEST::TEST(){	test[1].function_pt = &A::Test_Function; //sets the pointer to the proper function	test[2].function_pt = &B::Test_Function; //this line will give an error}void TEST::function(){	A *ptr;	 //I dont want this specific to class A though, I want it to be able to access			//any class of my choosing durning runtime	(ptr->*function_pt)();  //calls the function class_A.Test_Function()}void main(){		test[1].function();	test[2].function();	cout<<"Made it to the end"<}      



now, what I want the output to be is:

Made it to the class_A.Test_Function
Made it to the class_B.Test_Function
Made it to the end


if I comment out the lines "test[2].function_pt = &B::Test_Function;" in TEST::TEST
and the line "test[2].function();" in void main(), then it will compile fine and run
just fine, but not give me the result I want.

Can anyone help me with this please?

Possibility

Edited by - Possibility on May 28, 2000 4:54:22 AM
To create a pointer to a member-function you need to do something like this:
class testClass{    char *myString;public:    testClass (char *ms) { this->myString = ms); }        void function1 () { printf ("%s\n", this->myString); }    void function2 () { printf ("Function2\n"); }};typedef void (testClass::* cPtr) ();  // makes cPtr a                                // Pointer to a void ()                               // function in testClass                               // Note the typedef...testClass *myTest = new testClass ("Testing...");cPtr myPointer = testClass::function1;cPtr myPointer2 = testClass::function2;// To call a function use:(myTest->*myPointer) ();// Equivalent to myTest->function1 ();// Or:(myTest->*myPointer2) ();// Equivalent to myTest->function2 (); 


Hope this helps.

-Neophyte
~Death awaits you all, with nasty, big, pointy teeth~



Edited by - Neophyte on May 28, 2000 9:57:28 AM
ok, first mistake in your prog:
you''re using test[1] and test[2]. that will definitly cause an access violation because you should use test[0] and test[1] (don''t tell me you dont know array index starts at 0)

and 1 other thing. I''m not sure about this one, but i think the functions inside the class are defined as _closure and that''s why you can''t point to them specifically, cuz you dont have the correct type. try
void _closure (*function_ptr)();
not sure if it''ll work

- pouya
--------------
The trick to flight is to throw yourself at the ground and miss!!!

This topic is closed to new replies.

Advertisement