03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7.5. FROM MONOMORPHIC TO POLYMORPHIC BEHAVIOR 219<br />

• Static type checking: All types are checked statically.<br />

• Static binding (prevents separate compilation):All types are bound statically.<br />

• Efficiency: Compile-time evaluation and static binding allow optimization and efficiencies<br />

not available with dynamic binding.<br />

7.5.3 Comparison of Static and Dynamic Polymorphism<br />

Here the main features from static and dynamic polymorphism are summarized:<br />

• Virtual function calls are slower during run time than function templates: A virtual<br />

function call includes an extra pointer dereference to find the appropriate method in the<br />

virtual table. By itself, this overhead may not be significant. Significant slowdowns can<br />

result in compiled code because the indirection may prevent an optimizing compiler from<br />

inlining the function and from applying subsequent optimizations to the surrounding code<br />

after inlining.<br />

• Run time dispatch versus compile-time dispatch: The run time dispatch of virtual functions<br />

and inheritance is certainly one of the best features of object-oriented programming.<br />

For certain kinds of components, run time dispatching is an absolute requirement, decisions<br />

need to be made based on in<strong>for</strong>mation that is only available at run time. When this<br />

is the case, virtual functions and inheritance are needed.<br />

Templates do not offer run time dispatching, but they do offer significant flexibility at<br />

compile time. In fact, if the dispatching can be per<strong>for</strong>med at compile time, templates offer<br />

more flexibility than inheritance because they do not require the template arguments types<br />

to inherit from some base class.<br />

• Code size: virtual functions are small, templates are big:: A common concern in templatebased<br />

programs is code bloat, which typically results from naive use of templates. Carefully<br />

designed template components need not result in significantly larger executable size<br />

than their inheritance-based counterparts.<br />

• The binary method problem: There is a serious problem that shows up when using inheritance<br />

and virtual functions to express operations that work on two or more objects.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!