On 18 Jan 2021 at 16:45, Rukayat A Erinfolami wrote: > > How do I go about altering the control flow of a program running in > Qemu's user mode emulation. > For instance, at the callsite of function A::f(), I want A::f() and > all the definitions of f() by the derived classes of A to be > executed. So if B derives from A, I want to execute both A::f() > and B::f() at that callsite. >
I believe this is not how inheritance in C++ is ever gonna work :-) If A is your base class, and you instantiate A, the object instance knows that it is an A, but does *not* know about any potential or actual derived classes of its very own class A, nor does it *care*. So, if you do this: A my_object; my_object.f(); ...exactly the A::f() is only ever gonna execute, as that is what you have asked for. And, IMO you have no way of enumerating all the possible derived classes there are in your program/project, and maybe try calling their overridden versions of f(). In fact, *how* would you want the overridden versions of f() even to work, if the instance is actually of class A, i.e. does not have any potential member data elements of the derived classes? Even if you somehow managed that (by forced typecasting on a pointer), for the general case you'd be asking for some hilarious C-style breakage - accesses to memory locations that are not allocated or are in fact used for other data etc. Just in case, for the sake of completeness of my explanation - you could do the following = the opposite of what you have asked for (?): class A { public: f(); } class B : public A { public: f(); // overrides A::f() } A::F() { // do something 1 } B::f() { // do something 2 A::f(); // you can call the overridden version of f() explicitly // do something 3 } int main() { B my_object; my_object.f(); } I.e. in an overridden function, member of a derived class, you can call the "original" version of the function being overridden, present in your base class. If you want exactly that, you must do it explicitly, as it won't ever happen automatically. Destructors and constructors are an exception to that rule (with some added candy possible for CTORs via the "constructor initializer list"). Quoting Bruce Eckel's "Thinking in C++": "You should keep in mind that constructors and destructors are the only places where this hierarchy of calls must happen (and thus the proper hierarchy is automatically generated by the compiler). In all other functions, only that function will be called (and not base-class versions), whether it´s virtual or not. The only way for base-class versions of the same function to be called in ordinary functions (virtual or not) is if you explicitly call that function." Source: http://web.mit.edu/merolish/ticpp/Chapter15.html http://web.mit.edu/merolish/ticpp/Contents.html Thinking in C++ is a pretty good book. Not your classic language reference guide, some parts are dated (the language has developed further), but the book still contains lots of great explanations. I believe that the stuff that you're asking about is generic to C++, has nothing to do with QEMU or even the operating system that you're using to run your C++ programs. Frank