5 ms·
You’ve described interfaces, not so much inheritance.
by dyarosla 9y ago
You’ve described interfaces, not so much inheritance.
- kbsletten 9y agoIn the language of the article, this is subtyping without subclassing. Sure Java/C# implement this as interfaces, but in C++ this would be a case of multiple inheritance. This is a good example of the article's main point that people mean a lot of different things by inheritance and get different value from the different applications of the concept.
- humanrebar 9y agoThere are at least a few ways to define and implement interfaces in C++ without touching inheritance.
- andrewflnr 9y agoStructures of function pointers and, what, string method names? My C++ is rusty, but that's all I can think of. I guess there's a lot of variations on structures of pointers...
- nostrademons 9y agoTemplates and compile-time polymorphism. This is what the STL is based upon. A forward iterator is any type for which the * and ++ operators are defined; you can use it in any template that makes use of just those syntactic forms, but will get a compiler error if you try to instantiate a template with a type that does not support those operators. Punning on memory layouts, a la PyObject_HEAD or some of the objects in V8. With this approach, you make sure that all related types have the same memory layout for a subset of fields at the beginning of the struct. Then, a reinterpret_cast on a pointer to one member of the type will yield the same values as a different member of the type, and so can be used generically by code that depends only upon those members. (You have to be very careful with this approach, because multiple inheritance and vtables will usually break it, but if you absolutely positively must have zero-overhead runtime polymorphism it's occasionally the best option.) #include and preprocessor magic to redefine symbols based on preprocessor defines. Used to be the old way to swap out different implementations of a cross-platform interface, but seems to have largely fallen out of favor in the last couple decades. Linker magic to accomplish the same, where the header file defines a common interface but the linker may link with one of several different object files to implement that interface at link time. This one is still fairly commonly used; Google and Facebook both have their own custom STL & libstdc++ implementations, and this is also commonly used to swap out eg. malloc implementations. dlopen & dlsym to do this at runtime. Header file defines the interface, dlsym gives you back a function pointer or set thereof for a plugin implementation.
- stephen_gareth 9y agoIn Sean Parent's 'Inheritance is the base class of evil' talk Sean outlines an interesting way to achieve runtime polymorphism without inheriting interfaces in C++. https://www.youtube.com/watch?v=bIhUE5uUFOA https://www.youtube.com/watch?v=bIhUE5uUFOA
- Paul_Dirac 9y agoBut he uses interface inheritance. You see it at 9:44. Where he creates the concept_t and int_model_t inherits from it.
- stephen_gareth 9y agoIndeed. But the original type isn't 'burdened' with the interface. I've used the technique for a simple audio signal processing chain.
- jayshua 9y agoCould you name a few? I was under the impression that inheritance was the only way.
- dwaltrip 9y agoCan you give a mechanical example of inheritance, so we can see the difference?
- baddox 9y agoI think the term “interface” is used in many related but different ways in software, but I think the most obvious mechanical analogy would be basically anything that uses a common “plug” or “adapter.” Like a vacuum cleaner with different attachments. Or even HDMI for video/audio.
- weberc2 9y agoYou're confusing inheritance with implementing an interface. I'm not sure there is an example in the real world, because the real world is concerned with function, not taxonomy. It hardly makes sense to say "cordless drill inherits from screw driver"; rather, they both implement the "drives screw" interface. Ontological inheritance really doesn't make any sense at all for modeling systems (sometimes ontologies are useful as data in a system, but probably never for modeling the system itself).
- baddox 9y agoI’m not confusing those things. I’m giving an example of an interface, as requested.
- deleted 9y ago[deleted]
- dllthomas 9y agoIt may have been edited, but at least presently the comment you replied to asks for an example of inheritance.
- baddox 9y agoOh rats, my mistake. It is harder to come up with s good example of mechanical inheritance. Something like the cassette tape adapter kinda feels like inheritance, in the sense that it can be substituted in for a normal cassette tape but provides its own unique functionality.
- sametmax 9y agoInheritance does allow you to avoid implementing part of the interface.