3 ms·
This article describes the OOP approach of changing, say, a Logger to an abstract class with a default implementation, without needing to modify code that uses
by amluto 2y ago
This article describes the OOP approach of changing, say, a Logger to an abstract class with a default implementation, without needing to modify code that uses it, and also describes this as something that could be done in C++. But this doesn’t work at all in C++ if there is ever a Logger on the stack!
Logger l;
That’s a Logger, and it will not magically become a subclass.
And changing this to go through pointers everywhere and to use virtual functions, in C++, is not very performant. A good JIT compiler may be able to effectively devirtualize it, but C++ compilers are unlikely to be able to do this effectively.
- MathMonkeyMan 2y agoIn the case of a Logger, though, it is likely passed around by reference or pointer. So users of the Logger (reference/pointer) do not have to change if its definition changes to that of an interface. The "Server" class, or whatever instantiates the Logger, will have to change, but that might not be such a big deal. So yes, the author was wrong about not having to modify any code, but likely you won't have to modify most code.