[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to alter control flow

From: Frantisek Rysanek
Subject: Re: How to alter control flow
Date: Tue, 19 Jan 2021 11:14:55 +0100

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;

...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

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 {

class B : public A {
        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;


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."



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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]