⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2implem.html

📁 C ++ in action
💻 HTML
字号:
<html>
<head>
	<title>Implementation Digression</title>
    <meta  name="description" content="Implementation of polymorphism in C++">
    <meta name="keywords" content="polymorphism, class, inheritance, virtual function, vtable, virtual table">
	<link rel="stylesheet" href="../../rs.css">
</head>

<body background="../../images/margin.gif" bgcolor="#FFFFDC">

<!-- Main Table -->
<table cellpadding="6">
    <tr>
    <td width="78">
	&nbsp;
    
	<td>


<h3>Implementation Digression</h3>

<p>Obviously, for polymorphism to work, the object itself must store some information about its actual type. Based on this type the call is dispatched to the appropriate implementation of the method. In fact, the object may store a pointer to a dispatch table through which virtual methods are called. And that's how it works.
 
<p>Every class that has at least one virtual method (a polymorphic class) has a hidden member that is a pointer to a <b><i>virtual table</i></b>. The virtual table contains the addresses of the virtual member functions for that particular class. When a call is made through a pointer or reference to such object, the compiler generates code that dereferences the pointer to the object抯 virtual table and makes an <b><i>indirect call</i></b> using the address of a member function stored in the table. 

<p align="CENTER"><img src="images/Image36.gif" width=360 height=126>

<p class=caption>Figure. The invisible member of a celestial body梩he pointer to the vtable. The first entry in the vtable is a pointer to a function that is the destructor of celestial body.


<p>An object of a derived class that has overridden the implementation of the virtual method has its pointer pointed to a different virtual table. In that table the entry corresponding to that particular method contains the address of the overridden member function. 

<p align="CENTER"><img src="images/Image37.gif" width=288 height=144>

<p class=caption>Figure 2-2 The vtable pointer of a star points to a different vtable. Its first entry is also a pointer to a function that serves as a destructor. Only this time it抯 the destructor of  a star.


<p>Notice that, based on the type of the pointer, the compiler has enough information to decide whether to generate an indirect call through a vtable, or a direct call or inline expansion. If the pointer is declared to point to a class that has virtual functions, and the call is made to one of these functions, an indirect call is automatically generated. Otherwise a direct call or inline expansion is done. In any case, the compiler must first see the declaration of the base class (presumably in one of the header files) to know which functions are virtual and which are not.


<h3>The Overhead</h3>

<p>Before you decide to use virtual functions everywhere without much thinking, I have to make you aware of the size and speed considerations. The space overhead of polymorphism is: one pointer per every instance of the class. There is an additional per-class overhead of a vtable, but it抯 not that important. One more pointer per object is not a lot when dealing with a small number of relatively large objects. It becomes significant, however, when dealing with a large number of lightweight objects. 

<p>A virtual function call is slower than a direct call and significantly slower than the use of an inline function. Again, you can safely ignore this overhead when calling a heavy-weight member function, But turning an inline function such as <var>AtEnd ()</var> into a virtual function may significantly slow down your loops.

<p>So don抰 even think of the idea of creating the class <var>Object</var>梩he mother of all objects梬ith a handy virtual destructor (and maybe one more integer field for some kind of a class ID [run-time typing!], plus some conditionally compiled debugging devices, etc.). Don抰 try to make it the root of all classes. In fact, if you hear somebody complaining about how slow C++ is, he or she is probably a victim of this Smalltalk syndrome in C++. Not that Smalltalk is a poor language. When size and speed are of no concern, Smalltalk beats C++ on almost all fronts. It is a truly object oriented language with no shameful heritage of the hackers

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -