Is it possible, in C++ or any other compiled language, to add functionality to a class without modifying the header?
In JavaScript I can add a function to an object after the object is created. Is anything even remote possible in C++?
A use example would be for me to provide a user with some routine.o file, and have them extend it, with something like
void routine::NeverBeforeDeclaredFunction() { ... }
This exact example isn’t allowed, but is anything similar? I’ve thought about letting this class have an array of function pointers, and have a user populate that array with their own function. But this doesn’t provide any advantage, such as access to private variables, or access to this.
No, you cannot do that at runtime nor at compile time in C++. At least not portably without many headaches. You can create a subclass, but then it’s not “the same class”. If the class is a template class, you could provide your own specializations (but then it’s a different type).
C++/CX (a Microsoft language extension to C++) allows for partial classes. This might be a solution to your problem, if you don’t care about portability. Note that it is done at compile-time.
Objective-C allows this through categories (compile time) and through the Objective-C runtime (runtime).