Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Virtual method table
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Invocation== A call to <code>d->f1()</code> is handled by dereferencing <code>d</code>'s <code>D::B1</code> vpointer, looking up the <code>f1</code> entry in the virtual method table, and then dereferencing that pointer to call the code. '''Single inheritance''' In the case of single inheritance (or in a language with only single inheritance), if the vpointer is always the first element in <code>d</code> (as it is with many compilers), this reduces to the following pseudo-C++: <syntaxhighlight lang="cpp"> (*((*d)[0]))(d) </syntaxhighlight> Where <code>*d</code> refers to the virtual method table of <code>D</code> and <code>[0]</code> refers to the first method in the virtual method table. The parameter <code>d</code> becomes the [[This (computer science)|"<code>this</code>" pointer]] to the object. '''Multiple inheritance''' In the more general case, calling <code>B1::f1()</code> or <code>D::f2()</code> is more complicated: <syntaxhighlight lang="cpp"> (*(*(d[0]/*pointer to virtual method table of D (for B1)*/)[0]))(d) /* Call d->f1() */ (*(*(d[8]/*pointer to virtual method table of D (for B2)*/)[0]))(d+8) /* Call d->f2() */ </syntaxhighlight> The call to <code>d->f1()</code> passes a <code>B1</code> pointer as a parameter. The call to <code>d->f2()</code> passes a <code>B2</code> pointer as a parameter. This second call requires a fixup to produce the correct pointer. The location of <code>B2::f2</code> is not in the virtual method table for <code>D</code>. By comparison, a call to <code>d->fnonvirtual()</code> is much simpler: <syntaxhighlight lang="cpp"> (*B1::fnonvirtual)(d) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)