2

Can I include or declare a function inside a structure? I am trying to achieve polymorphism in C. If defining a function isn't the correct way to do it, what other methods can I use?

user2847425
  • 43
  • 1
  • 6

1 Answers1

4

Polymorphism as a feature of object-oriented languages is not available in C. Neither are encapsulation and inheritance - the language does not have the corresponding features.

This does not mean, however, that it is impossible to model the corresponding behavior with the regular features of C: it is possible to build a library that lets you produce behavior that looks like polymorphism, for example, by using arrays of function pointers.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523