Class template (C++)

1. Introduction

The definition and usage of class template and function template are similar. Sometimes, there are two or more classes whose functions are the same, only the data types are different.

  • The class template is used to realize the type parameterization of the data required by the class
  • Class templates are particularly important in representing data structures such as arrays, tables, and graphs. The representation and algorithms of these data structures are not affected by the data types included.

2. Grammar

The writing of the class template in C++ is as follows:

template <类型参数表>
class 类模板名{
    
    
  成员函数和成员变量
};

The type parameter table is written as follows:

class类塑参数1, class类型参数2, ...

The syntax for writing member functions in a class template outside the class template definition is as follows:

template <类型参数表>
返回值类型 类模板名<类型参数名列表>::成员函数名(参数表)
{
    
    
  ...
}

The way to define an object with a class template is as follows:

类模板名<真实类型参数表> 对象名(构造函数实际参数表);

If the class template has a parameterless constructor, you can also use the following writing:

类模板名 <真实类型参数表> 对象名;

Example:

template<class T>
class Person{
    
    
public:
	Person(T id,T age){
    
    
		this->mAge = age;
		this->mId = id;
	}
	void Show(){
    
    	
		cout << "ID:" << mId << " Age:" << mAge << endl;
	}
public:
	T mId;
	T mAge;
};
void main(){
    
    	
	//函数模板在调用的时候,可以自动类型推导
	//类模板必须显式指定类型
	Person<int> p(15,22);
	p.Show();
}

Third, the derivation of class templates

  1. Ordinary class derived class template
  2. A class template can also be derived from a class template. In this case, the parameter table of the derived class template should contain the parameters of the base class template.
  3. The template class has multiple inheritance like ordinary classes, that is, multiple inheritance is allowed between template classes.

New classes can be derived from class templates, either from class templates or non-template classes.

//类模板派生普通类
template<class T>
class Person{
    
    
public:
	Person(){
    
    
		mAge = 0;
	}
public:
	T mAge;
};
//类区定义对象,这个对象需要编译分配内存,要指定类型
class SubPerson : public Person<int>{
    
    };
//类模板派生类模板
template<class T>
class Animal{
    
    
public:
	void J(){
    
    
		cout << mAge << "动物在叫!" << endl;
	}
public:
	T mAge;
};

template<class T>
class Cat : public Animal<T>{
    
    };

int main(void)
{
    
    	
	Cat<int> cat;//指定类型
	return 0;
}

Fourth, the implementation of member functions outside the class template

template<class T>
class Person{
    
    
public:
	Person(T age, T id);
	void Show();
private:
	T mAge;
	T mID;
};
//外部实现函数
template<class T>
Person<T>::Person(T age, T id){
    
    
	this->mID = id;
	this->mAge = age;
}
template<class T>
void Person<T>::Show(){
    
    
	cout << "Age:" << mAge << " ID:" << mID << endl;
}

Five, class template compilation

First, a compilation unit ( - Search.com Unit ) refers to a * .cpp file and it #include all .h files, .h file code will be expanded to include its .cpp file, then compiler The .cpp file is an .obj file (assuming our platform is win32), the latter has the PE* ( Portable Executable , that is, windows executable file) file format, and it contains binary code, but it may not be able to Execution, because there is no guarantee that there must be a main function. After the compiler compiles all the *.cpp files in a project in a separate way, they are connected by the linker to become an .exe* file.

Just like a class, a class template can put its implementation and declaration together, or it can separate the interface from the implementation. However, due to historical reasons, the compiler's support for separate compilation is very weak, and the support strength varies depending on the platform.

STL's class templates are all put together with the interface and the implementation, and they can follow the mainstream. (The file is saved as a *** class template.hpp***, the essence of which is to mix the implementation code of .cpp into the .h header file. The definition and implementation are contained in the same file, and the caller of this class only needs to include the hpp file, no need to add cpp to the project to compile.)

Six, the static keyword in the class template

The static member variables of the class with different type parameters are independent of each other, which is determined by the implementation mechanism of the class template.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

template<class T>
class Person{
public:
	static int a;
};
template<class T> int Person<T>::a = 0;//类外初始化

int main(void)
{
	Person<int> p1, p2, p3;
	Person<char> pp1, pp2, pp3;
	p1.a = 10;
	pp1.a = 100;
	cout << p1.a << " " << p2.a << " " << p3.a << endl;//输出10 10 10
	cout << pp1.a << " " << pp2.a << " " << pp3.a << endl;//输出100 100 100
	return 0;
}

It can be seen that the static members between the objects of the class template corresponding to the same type such as int are shared, and the static members between the objects of the class template corresponding to different types such as int, float, and char are not shared.

Guess you like

Origin blog.csdn.net/weixin_45341339/article/details/111993632