Main Content

C - structs and unions, C++ classes and polymorphism

Archive - Originally posted on "The Horse's Mouth" - 2008-03-13 18:28:44 - Graham Ellis

C's structs allow you to define a variable type in which you can hold a variety of elements by name ... and that's very similar to how you define all the variables within a C++ class. In a C++ class, though, you also define your methods which take that language forward into the Object Oriented world.

C's unions let you define a block of memory which has two (or more) names associated with it so that - when combined with structs - you can produce a whole family of defined variable types based on a common core, but then varying in parts where appropriate. In OO terms, that's starting to head towards C++'s inheritance and polymorphism ...