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

void print2(C<T>& c) { copy(c.begin(), c.end(),

ostream_iterator<T>(cout, " ")); cout << endl;

}

int main() {

vector<string> v(5, "Yow!"); print1(v);

print2(v); } ///:~

Member function templates

It’s also possible to make apply( ) a member function template of the class. That is, a separate template definition from the class’ template, and yet a member of the class. This may produce a cleaner syntax:

dogs.apply(&Gromit::sit);

This is analogous to the act (in Chapter XX) of bringing ordinary functions inside a class.14

The definition of the apply( ) functions turn out to be cleaner, as well, because they are members of the container. To accomplish this, a new container is inherited from one of the existing STL sequence containers and the member function templates are added to the new type. However, for maximum flexibility we’d like to be able to use any of the STL sequence containers, and for this to work a template-template must be used, to tell the compiler that a template argument is actually a template, itself, and can thus take a type argument and be instantiated. Here is what it looks like after bringing the apply( ) functions into the new type as member functions:

//: C03:applyMember.h

//applySequence.h modified to use

//member function templates

template<class T, template<typename> class Seq> class SequenceWithApply : public Seq<T*> { public:

// 0 arguments, any type of return value:

14 Check your compiler version information to see if it supports member function templates.

Chapter 15: Multiple Inheritance

135

template<class R>

void apply(R (T::*f)()) { iterator it = begin(); while(it != end()) {

((*it)->*f)(); it++;

}

}

//1 argument, any type of return value: template<class R, class A>

void apply(R(T::*f)(A), A a) { iterator it = begin(); while(it != end()) {

((*it)->*f)(a); it++;

}

}

//2 arguments, any type of return value: template<class R, class A1, class A2> void apply(R(T::*f)(A1, A2),

A1 a1, A2 a2) { iterator it = begin(); while(it != end()) {

((*it)->*f)(a1, a2); it++;

}

}

}; ///:~

Because they are members, the apply( ) functions don’t need as many arguments, and the iterator class doesn’t need to be qualified. Also, begin( ) and end( ) are now member functions of the new type and so look cleaner as well. However, the basic code is still the same.

You can see how the function calls are also simpler for the client programmer:

//: C03:applyGromit2.cpp // Test applyMember.h #include "Gromit.h" #include "applyMember.h" #include <vector> #include <iostream> using namespace std;

int main() {

Chapter 15: Multiple Inheritance

136

Соседние файлы в предмете Численные методы
  • #
    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