C++ Polymorphism with no inheritance

Different products but same messages


Working Wandbox-ed modern C++ code, is HERE.

C++ Polymorphism with no inheritance is by no means, somewhat advanced concept. But, this is just because it is (in C++) based on different concepts v.s. some other good and popular languages where it is in the foundations  of the language design.

Lessons from GO

For example in GO LANG polymorphism is definitely not tied to inheritance. And this is one very much popular and good programing language, indeed.

I have prepared “earlier” one (I hope) succinct and useful GO LANG example of interfaces, types and messages dancing together.

GO is simple and that is interactive online code. So do not be afraid and at least try it once. I have tried to comment it extensively.

GO LANG OO concepts are inheriting (pun intended) from SmallTalk. Not from C++ or Simula. GO interface, can be seen conceptually as a collection of messages.

After the above, GO structs (types) can implement the message “Start”. They do not inherit the interface in order to implement it, as one does in C++. In GO you implement the message (aka method) that GO interface exhibits.


There is no interface keyword in C++. Usually, C++ interface is Abstract Base Class (ABC). Admittedly (at least to me) GO LANG ABC’s are an unnecessarily complex subject and we better quickly jump back to standard C++ we know and love (well at least some of us).

Slight Detour

Subtyping is actually what C++ developers think of as polymorphism. Common base class as a parent to subtypes implementing the same interface of the said common base.

Parametric Polymorphism is what C++ developers call “generic programming”. In “typeless” languages (think JavaScript) that kind of polymorphism is actually transparent. It just happens. (some are calling this ‘delegation’ but I do not think that is the right name to use in this context)

some_object above is not any kind of base class. It is “just” an object. In JavaScript, an object is implemented as a collection (dynamic array) of functions.