892
The C++ learning process
(aussie.zone)
Post funny things about programming here! (Or just rant about your favourite programming language.)
Perhaps we have a terminology mismatch, I tend to use abstract class and interface interchangeably. I’m not sure it’s possible to define a class interface in c++ without using inheritance, what kind of interface are you referring to that doesn’t use inheritance?
You do have a terminology mismatch. In C++, an abstract class is a class with at least one pure virtual method.
Such classes cannot be instantiated, so they are useful only as base classes.
An interface is more of a concept than a thing.
Sure you can say that
Iterable
is an interface that provides theNext()
andPrev()
methods and you can say thatArray
is anIterable
because it inherits fromIterable
(and then you override those methods to do the correct thing), and that’s one way to implement an interface in C++.But you can also say that
Iterable<T>
is a class template that provides aNext()
andPrev()
methods that call the methods of the same name on the type that they wrap (CRTP aka static polymorphism).Or you can say that an algorithm that scans a collection
T
forward requires the collection to have aNext()
method by callingNext()
on it.And I can think of at least 2 other ways to define an interface that isn’t using abstract classes.
And even if using abstract classes, inheriting from them is definitely the least flexible way to use them to define an interface, because it doesn’t allow one to do something like mocking functionality in tests, because it’s not possible to redefine the class to be tested to inherit from the test interface implementation with mocked functionality, so one still needs something to the effect of dependency injection anyway.
So yeah, abstract class is very different from inheritance, and it’s also very different from interface, even though it relates to both.
I agree, my terms aren’t perfect, but as you stated there isn’t really such a thing as an interface in c++, traditionally this is achieved via an abstract base class which is what I meant by using them interchangeably.
I know there are many things you can do in c++ to enforce an interface, but tying this back to the original comment that inheritance is objectively bad, I don’t think there’s any consensus that this is true. Abstract base classes (with no data members) and CRTP are both common use cases of inheritance in modern C++ codebases and are generally considered good design patterns.
Meh. Been developing professionally with C++ for 10 years at this point. I’m one of the weird people that kinda likes C++ and its pragmatism despite all its warts.
I’d like C++ better if it didn’t have inheritance. There are better solutions to model interfaces, and without inheritance people can’t write class hierarchies that are 10 levels deep with a different set of virtual functions overridden (and new virtual functions added) at each level.
And yes, that is not hypothetical. Real codebases in the real world shipping working products do that, and it’s about as nice as you can imagine.
I don't think it's what the person you're replying to meant, but template metaprogramming in modern c++ allows the use of "duck typing" aka "static polymorphism" where you can code against an interface without requiring inheritance.
Typically this is done with CRTP which does require inheritance. But I agree, you can do some meta programming or use concepts which can enforce interfaces in a different way. But back to the original comment that interfaces via inheritance are objectively bad, I don’t think there’s any consensus that this is true. And pure virtual interfaces and CRTP are both common use cases of inheritance in modern C++ codebases and are generally considered good design patterns.