Tag: Polymorphism
13 posts
-
Missing Virtual Destructor: Why Deleting Through a Base Pointer Leaks or Worse
Why deleting via a base pointer without a virtual destructor is undefined behavior, what GCC warns about, why unique_ptr<Base> hits it but shared_ptr hides it.
-
C++ Slicing Bugs: Finding Where a Base-Type Copy Dropped Your Derived Object
How C++ object slicing silently drops derived data in by-value parameters, vector<Base>, catch by value and base assignment, and how to make it a compile error.
-
Go in 2 Weeks #04: Interfaces, Duck Typing, any and Type Assertions
Go interfaces vs C++ virtual functions: implicit satisfaction, method sets and receivers, io.Reader/io.Writer, small interfaces, any, type assertions, and type switches.
-
C++ Virtual Functions: Polymorphism, override, and Pure
Virtual functions in C++: dynamic dispatch, virtual vs non-virtual, pure virtual and abstract classes, virtual destructors, vtables, and slicing pitfalls.
-
C++ Visitor Pattern: Classic Double Dispatch vs std::variant and std::visit
Visitor pattern in modern C++: classic accept/visit vs std::variant + std::visit (C++17), ASTs, the expression-problem tradeoff between adding types and adding operations, and the pitfalls in each.
-
C++ VTable Explained: Virtual Function Tables & Dynamic Dispatch
How C++ vtables and vptrs implement polymorphism: indirect calls, object size, multiple inheritance costs, the missing-virtual-destructor leak, and optimization with final and NVI.
-
Type Erasure in C++: How std::function and std::any Work, and Building Your Own
How std::function and std::any hide concrete types behind one interface, building your own copyable type-erased wrapper, and its cost versus virtuals.
-
The Strategy Pattern in C++: Virtual Classes vs Function Pointers vs Lambdas vs std::function
Strategy pattern in C++: polymorphic strategies, function pointers, lambdas, std::function—sorting and compression examples; performance trade-offs.
-
C++ Virtual Functions and vtables: How Dynamic Binding Works
How C++ virtual functions work under the hood: vtables, vptrs, static vs dynamic binding, override and final, pure virtual, object slicing.
-
C++ Object Slicing: Why Derived State Disappears and How to Prevent It
Why copying a derived object into a base value drops its state and overrides, where slicing hides (catch clauses, vectors, throw e;), and how to block it.
-
C++ Inheritance Design: Access Specifiers, Virtual Destructors, Hiding and Diamonds
C++ class hierarchy design: public vs private inheritance, virtual destructors, virtual calls in constructors, name hiding, slicing and virtual bases.
-
CRTP vs Virtual Functions: Static Polymorphism Without vtable Overhead
How CRTP gives C++ static polymorphism without a vtable, how it compares with virtual functions, the usual CRTP mistakes, and C++23 deducing this.
-
Factories in C++: Simple Factory vs Factory Method vs Abstract Factory, and Self-Registering Plugins
Simple Factory, Factory Method and Abstract Factory in C++ with unique_ptr, plus self-registering plugin factories and why static libraries silently drop them.