Function Pointers
how do function pointers work? i''d like to render different objects that are in function calls, but still use other accompanying code that is similar for all objects. would i be able to use function pointers to choose which object rendering function i choose among the other code?
(i know it sounds sorta confusing. sorry.)
a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
You should use virtual functions. You might want to do function pointers just to understand what the virtual functions are doing for you, but virtual functions should be the goal. A function name is actually a pointer just like an array name is actually a pointer to the start of the function. Not that there is some location in memory holding the address like a pointer, but rather that the compiler replaces it with the address. You declare a function pointer by int (*MyFuncPtr)(void). Now you can use it like any other pointer, but can also call it, i.e. i = MyFuncPtr(). The () basically serves the same purpose as the [] on arrays so you don''t need the *.
With a virtual function it is doing basically the same thing. The base class has a pointer to the it''s virtual functions. When a derived class declares the virtual function again the address of it''s function replaces the one in the base class. If you call that function in an instance of the base class then you will execute the function in the base class. If you do that same thing with an instance of the derived class you will call the function in the derived class. Just to clearify it matters what is actually pointed to, not the pointer, i.e. baseclass *myptr = new derivedclass() is actually pointing to the derived class and myptr->virtualfunc() will call the function in the derived class.
With a virtual function it is doing basically the same thing. The base class has a pointer to the it''s virtual functions. When a derived class declares the virtual function again the address of it''s function replaces the one in the base class. If you call that function in an instance of the base class then you will execute the function in the base class. If you do that same thing with an instance of the derived class you will call the function in the derived class. Just to clearify it matters what is actually pointed to, not the pointer, i.e. baseclass *myptr = new derivedclass() is actually pointing to the derived class and myptr->virtualfunc() will call the function in the derived class.
Keys to success: Ability, ambition and opportunity.
quote: Original post by LilBudyWizer
You should use virtual functions. You might want to do function pointers just to understand what the virtual functions are doing for you, but virtual functions should be the goal. A function name is actually a pointer just like an array name is actually a pointer to the start of the function. Not that there is some location in memory holding the address like a pointer, but rather that the compiler replaces it with the address. You declare a function pointer by int (*MyFuncPtr)(void). Now you can use it like any other pointer, but can also call it, i.e. i = MyFuncPtr(). The () basically serves the same purpose as the [] on arrays so you don't need the *.
With a virtual function it is doing basically the same thing. The base class has a pointer to the it's virtual functions. When a derived class declares the virtual function again the address of it's function replaces the one in the base class. If you call that function in an instance of the base class then you will execute the function in the base class. If you do that same thing with an instance of the derived class you will call the function in the derived class. Just to clearify it matters what is actually pointed to, not the pointer, i.e. baseclass *myptr = new derivedclass() is actually pointing to the derived class and myptr->virtualfunc() will call the function in the derived class.
Of course, the virtual function way is OOP only. If you are using structural programming, then you will need to declare the pointers like he said...
// Pointer to a function that returns void, and has void as args.void (*fpToto)(void);// Pointer to a function that returns a pointer to void.void* (*fpTata)(void);// Pointer to a function that returns a pointer to a function //(taking int as args) that returns an array of pointer to // integers.int* (*(*fpTiti)(void))[](int);// You can go with this to infinite...typCustom* (*(*fpTutu)(int*(*)(),int,typCustom*(*)[](int)))[](typCustom2*(*)[](int,void(*)(char*,...)));// Need help?// Pointer fpTutu to a function that takes as arguments:// 1 - A pointer to a function that returns int*// and takes void as argument.// 2 - An integer.// 3 - A pointer to a function that takes an integer // as argument and returns an array of pointers// to a type typCustom.// It returns a pointer to a function that returns an array// of pointers to typCustom and takes as argument:// 1 - A pointer to a function that need an integer// and a pointer to a function that takes a// string (pointer to char) and any arguments// (like the printf function), returning a // pointer to an array of pointers to typCustom.
Ok ok ok just fiddling with pointers. I love those!
Of course, the last kind of function is totally the proof of a REALLY defective programming habit and/or the baddest design I'd ever see, but the compiler will accept it and if you handle it correctly, there won't be any errors.
Now I know what I'm made of, and I'm afraid of it...
Edited by - Poltras on November 9, 2000 5:04:33 PM
Now I know what I'm made of, and I'm afraid of it...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement