views:

187

answers:

2

Hey folks,

I just read this http://stackoverflow.com/questions/2172887/use-c-struct-in-objective-c question, and I was wondering why anyone would want to use function pointers in structs. Wouldn't wrapping the functions in a class be equivalent?

+1  A: 

Function pointers are used like that in C for creating polymorphic behavior, sort of a poor man's object. The only reasons I can think of doing this other than conforming to an existing C library is to gain a performance advantage over objective-c method dispatch.

ergosys
+1  A: 

I don't think that you would need anything like that in objective-c.

That's how you would implement polymorphism in C. It's like defining an interface. Some frameworks (including kernel? filesystem?) would expect you to pass in a structure with your functions which the framework would than call when some event happened.

If you really really need I think you could use c++ and struct/classes from objective-c instead of using plain C structures with function pointers but you should be okay with using objective-c.

stefanB