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!
==Example== Consider the following class declarations in [[C++ syntax]]: <syntaxhighlight lang="cpp"> class B1 { public: virtual ~B1() {} void fnonvirtual() {} virtual void f1() {} int int_in_b1; }; class B2 { public: virtual ~B2() {} virtual void f2() {} int int_in_b2; }; </syntaxhighlight> used to derive the following class: <syntaxhighlight lang="cpp"> class D : public B1, public B2 { public: void d() {} void f2() override {} int int_in_d; }; </syntaxhighlight> and the following piece of C++ code: <syntaxhighlight lang="cpp"> B2 *b2 = new B2(); D *d = new D(); </syntaxhighlight> g++ 3.4.6 from [[GNU Compiler Collection|GCC]] produces the following 32-bit memory layout for the object <code>b2</code>:<ref group="nb">G++'s <code>-fdump-class-hierarchy</code> (starting with version 8: <code>-fdump-lang-class</code>) argument can be used to dump virtual method tables for manual inspection. For AIX VisualAge XlC compiler, use <code>-qdump_class_hierarchy</code> to dump class hierarchy and virtual function table layout.</ref> <pre> b2: +0: pointer to virtual method table of B2 +4: value of int_in_b2 virtual method table of B2: +0: B2::f2() </pre> and the following memory layout for the object <code>d</code>: <pre> d: +0: pointer to virtual method table of D (for B1) +4: value of int_in_b1 +8: pointer to virtual method table of D (for B2) +12: value of int_in_b2 +16: value of int_in_d Total size: 20 Bytes. virtual method table of D (for B1): +0: B1::f1() // B1::f1() is not overridden virtual method table of D (for B2): +0: D::f2() // B2::f2() is overridden by D::f2() // The location of B2::f2 is not in the virtual method table for D </pre> Note that those functions not carrying the keyword <code>virtual</code> in their declaration (such as <code>fnonvirtual()</code> and <code>d()</code>) do not generally appear in the virtual method table. There are exceptions for special cases as posed by the [[default constructor]]. Also note the [[Virtual function#Virtual destructors|virtual destructors]] in the base classes, <code>B1</code> and <code>B2</code>. They are necessary to ensure <code>delete d</code> can free up memory not just for <code>D</code>, but also for <code>B1</code> and <code>B2</code>, if <code>d</code> is a pointer or reference to the types <code>B1</code> or <code>B2</code>. They were excluded from the memory layouts to keep the example simple. <ref group="nb">{{Cite web|url=https://stackoverflow.com/questions/17960917/why-there-are-two-virtual-destructor-in-the-virtual-table-and-where-is-address-o|title = C++ - why there are two virtual destructor in the virtual table and where is address of the non-virtual function ๏ผgcc4.6.3๏ผ}}</ref> [[Method overriding|Overriding of the method]] <code>f2()</code> in class <code>D</code> is implemented by duplicating the virtual method table of <code>B2</code> and replacing the pointer to <code>B2::f2()</code> with a pointer to <code>D::f2()</code>.
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)