Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
B.Eckel - Thinking in C++, Vol.2, 2nd edition.pdf
Скачиваний:
50
Добавлен:
08.05.2013
Размер:
2.09 Mб
Скачать

Pointer specialization

Partial ordering of function templates

Design & efficiency

In Sorted, every time you call add( ) the element is inserted and the array is resorted. Here, the horribly inefficient and greatly deprecated (but easy to understand and code) bubble sort is used. This is perfectly appropriate, because it’s part of the private implementation. During program development, your priorities are to

1.Get the class interfaces correct.

2.Create an accurate implementation as rapidly as possible so you can:

3.Prove your design.

Very often, you will discover problems with the class interface only when you assemble your initial “rough draft” of the working system. You may also discover the need for “helper” classes like containers and iterators during system assembly and during your first-pass implementation. Sometimes it’s very difficult to discover these kinds of issues during analysis

– your goal in analysis should be to get a big-picture design that can be rapidly implemented and tested. Only after the design has been proven should you spend the time to flesh it out completely and worry about performance issues. If the design fails, or if performance is not a problem, the bubble sort is good enough, and you haven’t wasted any time. (Of course, the ideal solution is to use someone else’s sorted container; the Standard C++ template library is the first place to look.)

Preventing template bloat

Each time you instantiate a template, the code in the template is generated anew (except for inline functions). If some of the functionality of a template does not depend on type, it can be put in a common base class to prevent needless reproduction of that code. For example, in Chapter XX in InheritStack.cpp inheritance was used to specify the types that a Stack could accept and produce. Here’s the templatized version of that code:

//: C03:Nobloat.h

// Templatized InheritStack.cpp #ifndef NOBLOAT_H

#define NOBLOAT_H

#include "../C0A/Stack4.h"

template<class T>

class NBStack : public Stack { public:

Chapter 15: Multiple Inheritance

141

void push(T* str) { Stack::push(str);

}

T* peek() const {

return (T*)Stack::peek();

}

T* pop() {

return (T*)Stack::pop();

}

~NBStack();

};

// Defaults to heap objects & ownership: template<class T>

NBStack<T>::~NBStack() { T* top = pop(); while(top) {

delete top; top = pop();

}

}

#endif // NOBLOAT_H ///:~

As before, the inline functions generate no code and are thus “free.” The functionality is provided by creating the base-class code only once. However, the ownership problem has been solved here by adding a destructor (which is type-dependent, and thus must be created by the template). Here, it defaults to ownership. Notice that when the base-class destructor is called, the stack will be empty so no duplicate releases will occur.

//: C03:NobloatTest.cpp #include "Nobloat.h" #include "../require.h" #include <fstream> #include <iostream> #include <string>

using namespace std;

int main(int argc, char* argv[]) { requireArgs(argc, 1); // File name is argument ifstream in(argv[1]);

assure(in, argv[1]); NBStack<string> textlines; string line;

// Read file and store lines in the stack:

Chapter 15: Multiple Inheritance

142

Соседние файлы в предмете Численные методы
  • #
    08.05.20133.99 Mб22A.Menezes, P.van Oorschot,S.Vanstone - HANDBOOK OF APPLIED CRYPTOGRAPHY.djvu
  • #
  • #
    08.05.20135.91 Mб24B.Eckel - Thinking in Java, 3rd edition (beta).pdf
  • #
  • #
    08.05.20136.09 Mб17D.MacKay - Information Theory, Inference, and Learning Algorithms.djvu
  • #
    08.05.20133.85 Mб15DIGITAL Visual Fortran ver.5.0 - Programmers Guide to Fortran.djvu
  • #
    08.05.20131.84 Mб12E.A.Lee, P.Varaiya - Structure and Interpretation of Signals and Systems.djvu