3

I just read this 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
  • 3
    It certainly is a C question. In C, function pointers are shoved into structs all the time and often into roles exactly like what class inheritance gives you in an OO language. Commented Feb 1, 2010 at 4:42

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.