Need for function pointers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have been trying to understand function pointers for a while but not able to figure out why we really need a function pointer. I understand what it does but why do we need them, why cant we call the function itself

Sorry for the naive question but I am lost
Thanks.
 
Puchi said:
I have been trying to understand function pointers for a while but not
able to figure out why we really need a function pointer. I understand
what it does but why do we need them, why cant we call the function
itself.

In OOP, function pointer aren't needed, it's not OOP.

In "classic" C, it's very useful, look at the qsort function for
instance: you want to sort an array, how to give the sort criteria
(numéric, date, character based, color, whatever) in a generic way? The
idea is to give the array to sort and the function used to compare two
element of this array as parameter of the sort function.

Internally, virtual member of a class are kind of function pointer. In
fact, lot of C application use function pointer to mimic some OOP
concept.

Note: The big issue with function pointer is that they're not typed,
it's very very easy to produce compilable but totally incorrect code and
hard to debug using function pointer. If you want to get an idea of a
possible better solution, look at C# (.NET) delegates.
 
The other thing that makes them useful is the fact that you don't need to know the function name to use them. Lets say you had functions for adding, subtracting, multiplying and dividing, you would need an if/else if/else block to decide which function to use. But if these functions have the same signature, same return and parameter list, you can pass the required function and avoid code bloat. This was important in the old days when you had 64K of memory to play with, but these days with mega and giga bytes of memory, you can afford the if/else stuff

Have a look at MultiCast delegates in VS as well, it'll show you how you can combine them into a list, like an array which is called in order, so the first will paint the background, the second will paste the image, etcâ€
 
Puchi said:
I have been trying to understand function pointers for a while
but not able to figure out why we really need a function pointer.
I understand what it does but why do we need them, why cant we
call the function itself.

Functions pointers are there to allow to
decide at run-time which function to call.
(If you call the function itself, this
will be fixed at compile-time.)
Generally, using virtual functions does
the same and it is less error prone, so
actually there is little need for function
pointers in C++ except the need to support
C APIs.
Sorry for the naive question but I am lost.
Thanks.

HTH,

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
In OOP, function pointer aren't needed, it's not OOP.
This can be debatable.
This is why functors where invented in C++.
Ok, I will not argue about the meanings/definitions here.
What I wanted to say is that there is a need of something working
like a pointer to a function, but safer and with more type info.
 
If you want to get an idea of
Delegates are available in C++.
I know cause I'm using them. They are much easier to use than function pointers.
 
Steve said:
Delegates are available in C++.
I know cause I'm using them. They are much easier to use than
function pointers.

Delegates are available in Managed Extensions for C++. They're not part of
the C++ language. You can get the same kind of functionality in pure
standard C++ using libraries like boost::function and boost::signals.

-cd
 
Just question Carl. Are delegates part of .net or are they actually
built into the languages. I've looked C++, C# and VB and they all seem
to implement them, so I've taken it for granted that they're
basically part of .net. Am I jumping the gun a bit?

They're part of .NET. But basically, it's just a class that hold a
reference to a method and an object. Language have to offer a "syntaxic
sugar" in order to allow developer to use delegate in an easy way (and
to make them smell more like a feature than a simple class construct).

For instance, if you look at boost, you can get the same functionality,
but it's a bit harder to write. I mentioned C# in my first post because
it's IMHO the easiest language to approach delegate if you're from C++
(MC++ contains too much managed specific stuff to use it as an
"approach" to delegates).
 
Cheers Quentin, I know what you mean about the ease of use. Its like a few years ago when C was basically the low level language compared to C++, and now C++ is the low level language compared to C#. Kind of a poetic justice, some would say "Don't use C, its like a knife edge, use C++".
 
Back
Top