3 ms·
> inheritance is not polymorphic in C++. Virtual methods are opt-in. Exactly, if you want polymorphism in C++, you have to drag in inheritance too. You can't h
by bheadmaster 3y ago
> inheritance is not polymorphic in C++. Virtual methods are opt-in.
Exactly, if you want polymorphism in C++, you have to drag in inheritance too. You can't have the former without the latter. In Go, you can have polymorphism through interfaces without ever embedding a single struct, so that would make Go more OOP than C++.
- kazinator 3y agoInterfaces are useless in the context of OOP because they don't bring in implementation (or anything else). The beauty of inheritance is implementation reuse. In a proper Alan-Kay-Blessed OOP language, the "interface" has no role. Why? Because there is extermely late binding everywhere. What that means is that if we want an object to be substitutable wherever a Duck is needed, we just need to make it quack like a Duck. It doesn't have to inherit anything. Interface inheritance is just syntax: a static type system imposed into OOP, which lets you do type checks. Oh! That object cannot be used here; though it quacks like a Duck, it doesn't inherit DuckInterface. Please add DuckInterface, then fix errors (if any) and try again. Now inheritance is actually useful even when we have the above True Scotsman's Late Binding OOP that doesn't impose static interface cruft on us. Because: 1. It lets you use implementation. Say that quacking like a duck is a complex set of behaviors. Let the specialists develop (and maintain) DuckBase: you just inherit that (and pull in updates once in a while). 2. It gives you a real instance-of dynamic relationship: your object can be asked "are you a duck" at the dynamic type level, and the type system will assure the caller, that "yes". You can't get that if your duck behavior is coming by delegation to an object contained in your DuckLike by composition. Also, only multiple inheritance is the real deal; single is seriously crippled.
- pjmlp 3y agoI wouldn't call COM useless, it has served as Windows OOP ABI quite well for the last 25 years, to the point of having been copied in some way to other platforms.
- kazinator 3y agoCOM isn't, and doesn't require interface inheritance. COM is based on registering a binary object which quacks in a certain way, under the right GUID symbol so it can be found.
- pjmlp 3y agoIt doesn't require interface inheritance, it supports interface inheritance. The lookup mechanism is a implementation detail, just like in any OOP language with interfaces.
- kazinator 3y agoCOM is an ABI, not a programming language. There is literally nothing in COM that does interface inheritance. The ABI for in-process COM is such that when you use Microsoft C++, you can use certain abstract base classes. Your objects will be binary compatible with COM.
- pjmlp 3y agoCOM is a programming language model exposed as ABI, with language projections, successfully used for 25 years across multiple languages. Basically it is like we are discussing how vtables work over here.
- gpderetta 3y agoNot necessarily. std::function for example provides polymorphism of callables without inheritance. Type erasure can generally used for polymoprhism in C++. Also templates are of course polymorphic and do not require inheritance, but that's not runtime polymorphism which I guess was implied. edit: s/is/can be/