[C ++ in-depth analysis] 43. The concept and meaning of class templates

Type 1 template

C ++ applies the idea of ​​templates to classes, so that the implementation of classes no longer focuses on the specific types of data elements, but only the functions that group classes need to implement. Some classes are mainly used to store and organize data elements, such as array classes, linked list classes, and Stack. Class, Queue class

Similarly, use template to identify before the class declaration, the syntax is as follows:
Insert picture description here

1.1 Application of class templates

  • Only specific types can be displayed, not automatically derived
  • Use the specific type <Type> to define the type

Insert picture description here

1.2 Processing of class templates by the compiler

  • Generate different classes by specific types
  • Compile the class template itself in the declared place
  • Compile the code after parameter substitution in the place of use
// 43-1.cpp
#include<iostream>
#include<string>
using namespace std;
template<typename T>
class Operator
{
public:
    T add(T a, T b) { return a + b; }
    T minus(T a, T b) { return a - b; }
    T multiply(T a, T b) { return a * b; }
    T divide(T a, T b) { return a / b; }
};
string operator - (string& l, string& r)	// 重载'-'操作符,这里就是为了编译通过
{
    return "Minus";
}
int main()
{
    Operator<int>op1;						// 显示指定具体类型
    cout << op1.add(1, 2) << endl;
    Operator<string>op2;					// 显示指定具体类型
    cout << op2.add("Let's ", "go!") << endl;
    cout << op2.minus("Let's ", "go!") << endl;
    return 0;
}

When the compiler compiles, it first compiles the class itself, such as lines 5-13; when creating a specific class, it also needs to be compiled, lines 20 and 22; when using a member function, it also needs to be compiled, such as line 21, Lines 23 and 24. If we do not overload the minus operator, line 24 will compile error, but line 22 can be compiled. This also shows that it needs to be compiled when generating a specific class, and also to call member functions.

$ g++ 43-1.cpp -o 43-1
$ ./43-1
3
Let's go!
Minus

Engineering application of type 2 templates

  • The class template must be defined in the header file
  • Class templates are implemented in the same file
  • The member functions defined outside the class template need to add the template <> declaration
// Operator.h
#ifndef _OPERATOR_H_
#define _OPERATOR_H_
template<typename T>
class Operator
{
public:
    T add(T a, T b);
    T minus(T a, T b);
    T multiply(T a, T b);
    T divide(T a, T b);
};
template<typename T>
T Operator<T>::add(T a, T b)
{
    return a + b;
}
template<typename T>
T Operator<T>::minus(T a, T b)
{
    return a - b;
}
template<typename T>
T Operator<T>::multiply(T a, T b)
{
    return a * b;
}
template<typename T>
T Operator<T>::divide(T a, T b)
{
    return a / b;
}
#endif
// 43-2.cpp
#include<iostream>
#include"Operator.h"
using namespace std;
int main()
{
    Operator<int>op1;
    cout << op1.add(1, 2) << endl;
    cout << op1.multiply(4, 5) << endl;
    cout << op1.minus(5, 6) << endl;
    cout << op1.divide(10, 5) << endl;
    return 0;
}

The declaration and definition of the class template should be in the same inquiry price, the member functions defined outside the class template need to add the template <> declaration

Compile and run

$ g++ 43-2.cpp -o 43-2
$ ./43-2
3
20
-1
2

3 Summary

1. The class template can only display the specified type
2. The compiler compiles where it is declared and where it is used
3. The class template is implemented in the same file

Published 298 original articles · praised 181 · 100,000+ views

Guess you like

Origin blog.csdn.net/happyjacob/article/details/104521592