C++ basic study notes 08-template

1. Template

Establish a universal mold to greatly improve the reusability.
Another programming idea of ​​C++ is generic programming. The main technology used is template
C++ provides two template mechanisms. Function template and class template.
1.1 Function template
function: Create a universal function. Function return value type and formal parameter type can not be specified, and a virtual type is used to represent the
syntax:
template
function declaration or definition
Explanation:
template——declaration to create template
typename——indicating that the symbol behind it is a data type. Use class instead of
T-general data type, the name can be replaced, usually uppercase

template<typename T>
void swap(T &a,T &b){
    
    
	T temp=a;
	a=b;
	b=temp;
}

Template call:

int a=10;
int b=20;
swap(a,b);	//自动类型转换
swap<int>(a,b);	//显示类型转换

1.1.1 Precautions for function templates
Automatic type derivation, the consistent data type T must be derived before it can be used. The
template must be used to determine the data type of T before it can be used.

1.1.2 The difference between
ordinary functions and function templates The difference between ordinary functions and function templates:
automatic type conversion can occur when ordinary functions are called. When a
function template is called, if automatic type inference is used, implicit type conversion will not occur.
If you use the display of the specified type Way, implicit type conversion can occur

1.1.3 Normal function calling convention with a function template
calls the rules are as follows:
If the function templates and common function can be achieved, priority call a normal function
can empty template parameter list to force the calling function templates
overloaded function template can also occur
if the function template Can produce better matches and call function templates first

1.2 Class template
Function: to establish a general class, the member data type in the class can be specified without specifying it, and a virtual type is used to represent it.
Syntax:
template
class
Explanation:
template——declares the template to be created
typename——means that the symbol behind it is one A data type, you can use class instead of
T-a general data type, the name can be replaced, usually in uppercase letters

template<class NameType,class AgeType>
class Person{
    
    
	public:
		Person(NameType name,AgeType age){
    
    
			m_name=name;
			m_age=age;
		}
		NameType m_name;
		AgeType m_age;
};
int main(){
    
    
	Person<string,int> p("Tom",18); //类模板只能显示指定类型
	cout<<p.m_name<<" "<<p.m_age<<endl;
	return 0;
}

1.2.1 The difference
between a class template and a function template There are two main differences: the
class template does not have an automatic type inference method. The
class template can have default parameters in the template parameter list.

1.2.2 When to create
a member function in a
class template The member function in an ordinary class can be created from the beginning . The member function in the class template is created when it is called

1.2.3 Class template object as function parameter
Three ways of passing in:
specify the type passed in-directly display the data type
of the object Parameter template-turn the parameters in the object into a template to pass the
entire class template-change This object type is templated for delivery

1.2.4 class template and inheritance
when the subclass inherits the parent class is a template class, subclass when it was declared, to specify the type of parent class T
If not specified, the compiler can not allocate memory to subclass
if you want to Flexibly specify the type of T in the parent class, and the subclass also needs to become a class template

template<class T>
class Base{
    
    
	T a;
};
//class Son:public Base  错误, C++编译器给子类分配内存,必须知道父类中T的类型才可以向下继承
class Son:public Base<int>{
    
    
}; 
template<class T1,class T2>
class Son2:public Base<T2>{
    
    
	T1 age;
};
int main(){
    
    
	Son2<int,char> s2;	//T1的数据类型为int,T2为char,制定了父类的数据类型为char 
	return 0;
}

1.2.5 Class template function implementation outside the
class When the member function in the class template is implemented outside the class, the template parameter list needs to be added

template<class T1,class T2>
class Person{
    
    
	public:
		T1 m_name;
		T2 m_age;
		Person(T1 name,T2 age);
		void showPerson();
};
template<class T1,class T2>
Person<T1,T2>::Person(T1 name,T2 age){
    
    	//成员函数类外实现 
	m_name=name;
	m_age=age;
}
template<class T1,class T2>
void Person<T1,T2>::showPerson(){
    
    	//成员函数类外实现 
	cout<<m_name<<" "<<m_age<<endl;
}
int main(){
    
    
	Person<string,int> p("Tom",18);	//注意类模板创建对象时要声明数据类型
	p.showPerson();
	return 0;
}

Summary:
When implemented outside the class template class: return value type class name<T1,T2>::function name(){}

1.2.6 Class template and friends
Realization in the global function class: directly declare friends in the
class. Realization out of the global function class: you need to let the compiler know the existence of the global function in advance.
Too complicated

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/104950067