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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

216 CHAPTER 7. EFFECTIVE PROGRAMMING: THE POLYMORPHIC WAY<br />

pointers or references to the objects have to be used, which eliminates the possibility <strong>for</strong> a<br />

compiler to optimize some parts of the code, i.e. inlining. Second, a dynamic cast has to be<br />

used which can cause an exception at run time. This kind of problem is called binary-methodproblem,<br />

which is explained in Section 7.5.3<br />

Nevertheless, dynamic polymorphism in <strong>C++</strong> is best at:<br />

• Uni<strong>for</strong>m manipulation based on base/derived class relationships: Different classes that<br />

hold a base/derived relationship can be treated uni<strong>for</strong>mly.<br />

• Static type checking: All types are checked statically in <strong>C++</strong>.<br />

• Dynamic binding and separate compilation: Code that uses classes in a hierarchy can<br />

be compiled apart from the code of the entire hierarchy. This is possible because of the<br />

indirection that pointers provide (both to objects and to functions).<br />

• Binary interfacing: Modules can be linked either statically or dynamically, as long as the<br />

linked modules lay out the virtual tables the same way.<br />

Behind the Dynamic Polymorphism in <strong>C++</strong><br />

How virtual functions work:<br />

• Normally when the compiler sees a member function call it simply inserts<br />

instructions calling the appropriate subroutine (as determined by the<br />

type of the pointer or reference)<br />

• However, if the function is virtual a member function call such as<br />

vc→ foo() is replaced with following: (∗((vc→ vtab)[0]))()<br />

• The expression vc→ vtab locates a special ”secret” data member of the<br />

object pointed to by vc. This data member is automatically present in<br />

all objects with at least one virtual function. It points to a class-specific<br />

table of function pointers (known as the class’s vtable)<br />

• The expression (vc→ vtab)[0] locates the first element of the class’s<br />

vtable of the object (the one corresponding to the first virtual function<br />

foo() ). That element is a function pointer to the appropriate foo()<br />

member function.<br />

• Finally, the expression (∗((vc→ vtab)[0]))() dereferences the function<br />

pointer and calls the function<br />

• Special care must be taken with destructors in virtual class hierarchies.<br />

The base class does not know anything about the derived classes and<br />

so the derived class destructor has to be marked with virtual, too.

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

Saved successfully!

Ooh no, something went wrong!