TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25011 17
A virtual pointer pattern for dynamic resolution in C++ — years in production

I've been working on Olex2, a crystallography software package, for over 20 years. At some point I needed pointers whose target wasn't a fixed address but a runtime decision — "whatever is currently the active object of this type."

The result was olx_vptr — a virtual pointer where resolution is delegated to a user-defined get_ptr():

https://github.com/pcxod/olex2/blob/master/sdl/olxvptr.h

The calling code uses natural pointer syntax and knows nothing about how resolution happens. A concrete use looks like this:

struct VPtr : public olx_virtual_ptr<TXFile> {

virtual IOlxObject *get_ptr() const;

};

olx_vptr<TXFile> thip(new VPtr());

lib->Register(
new TFunction<TXFile>(thip, &TXFile::LibGetFormula, "GetFormula", .....

(https://github.com/pcxod/olex2/blob/master/xlib/xfiles.cpp#L1427)

Single virtual dispatch, fully type-safe, open to any resolution strategy.

I'm surprised this pattern never made it into the standard or common literature. Has anyone seen something similar? Would there be interest in a more formal writeup?

https://redd.it/1spvxx6
@r_cpp
GitHub GitHub - pcxod/olex2 Contribute to pcxod/olex2 development by creating an account on GitHub.
More from @r_cpp
  1. Oct 6, 2026C++20 modules support: 228 projects already available to be consumed Hello everyone. I che…
  2. Oct 6, 2026operator<>: A Dream of Higher-order Metaprogramming Parity https://www.elbeno.com/blog/?p=…
  3. Oct 6, 2026University of Waterloo user study on understanding CMake changes Hi everyone, I’m Mattie N…
  4. Oct 6, 2026Try to Implement Reflection Annotations in C++20 https://typecombinator.github.io/en/posts…
  5. Oct 5, 2026New C++ Conference Videos Released This Month - September 2026 (Updated To Include Videos…
  6. Oct 5, 2026What’s New for C++ Developers in Visual Studio 2026 (18.7 – 18.10) https://devblogs.micros…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →