Possible Duplicate:
How can I simulate OO-style polymorphism in C?
I’m trying to better understand the idea of polymorphism with examples from languages I know;
is there polymorphism in C?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is Nekuromento’s second example, factored in the way I consider idiomatic for object-oriented C:
animal.h
cat.c
dog.c
main.c
This is an example of runtime polymorphism as that’s when method resolution happens.
C1x added generic selections, which make compile-time polymorphism via macros possible. The following example is taken from the C1x April draft, section 6.5.1.1 §5:
Type-generic macros for math functions were already available in C99 via the header
tgmath.h, but there was no way for users to define their own macros without using compiler extensions.