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.

74 CHAPTER 3. CLASSES<br />

a real number, the implicit conversion does not create semantic inconsistencies. An implicit<br />

constructor is more convenient because doubles and double literals can be given whereever a<br />

complex is expected. Functions that are not per<strong>for</strong>mance-critical can be implemented only once<br />

<strong>for</strong> complex and used <strong>for</strong> double. Vice versa, in per<strong>for</strong>mance-critical applications it might be<br />

preferable using an explicit constructor because the compiler will refuse to call complex functions<br />

with double arguments. Then the programmer can implement overload of those functions with<br />

double arguments that do not waste run time on null imaginaries.<br />

That does not mean that high-per<strong>for</strong>mance implementations necessarily have to be realized with<br />

explicit constructors. The implicit conversion might happen in rarely called functions and the<br />

impact on the overall per<strong>for</strong>mance might be negligible. The compiler cannot tell us but a profiling<br />

tool can. A function that consumes less than 1 % of the execution time is not worth to spend<br />

much time on tuning it. All this considered, there are more reasons <strong>for</strong> an implicit constructor<br />

than <strong>for</strong> an explicit one and so it is implemented in std::complex.<br />

3.4 Destructors<br />

A destructor is a function that is called every time an object of this class is destroyed, <strong>for</strong><br />

example:<br />

∼complex()<br />

{<br />

std::cout ≪ ”So long and thanks <strong>for</strong> the fish.\n”;<br />

}<br />

Since the destructor is the complementary operation of the default constructor it uses the<br />

complementary notation in the signature. Opposed to the constructor there is only one single<br />

overload and arguments are not allowed — what could they are good <strong>for</strong> anyway, as grave<br />

goods? There is no live after death in C ++.<br />

In our example, there is nothing to do when a complex number is destroyed and we can omit<br />

the destructor. A destructor is needed when the object acquired resources, e.g. memory. In<br />

this cases the memory must be freed in the destructor and the other ressource be released.<br />

class vector<br />

{<br />

public:<br />

// ...<br />

∼vector()<br />

{<br />

if (data) // check if pointer was allocated<br />

delete[] data;<br />

}<br />

// ...<br />

private:<br />

unsigned my size;<br />

double ∗data;<br />

};<br />

Files that are opened with std::ifstream or std::ofstream does not need to closed explicitly, their<br />

destructors will do this if necessary. Files that are opened with old C handles require explicit<br />

closing and this is only one reason <strong>for</strong> not using them.

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

Saved successfully!

Ooh no, something went wrong!