[C++] class template

1. Class template syntax

Class template function:

  • Establish a general class, the member data type in the class can not be specified specifically, represented by a virtual type .

grammar:

template<typename T>
类

Explanation:

template --- Declare to create a template

typename --- The symbol behind it is a data type, which can be replaced by class

T --- General data type, the name can be replaced, usually in uppercase letters

#include <iostream>
#include <string>
using namespace std;

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;

	void printInfo()
	{
		cout << "姓名是:" << this->m_Name << "  年龄是:" << this->m_Age << endl;
	}
};

void test01()
{
	//通过类模版来实例化对象
	Person <string, int> person("张三", 33);
	person.printInfo();
}

int main(void)
{
	test01();

	system("pause");
	return 0;
}

Summary: The syntax of a class template and a function template is similar. Add a class after the declaration template template. This class is called a class template

2. The difference between class template and function template

There are two main differences between a class template and a function template:

      (1) There is no way to use automatic type inference for class templates

      (2) The class template can have default parameters in the template parameter list

#include <iostream>
#include <string>
using namespace std;

//2. 类模板在模板参数列表中可以有默认参数
template <class nameType,class ageType = int>
class Person2
{
public:
	Person2(nameType name,ageType age) :m_Name(name), m_Age(age)
	{

	}
	void showInfo()
	{
		cout << "name:" << this->m_Name << "  age:" << this->m_Age << endl;
	}

	nameType m_Name;
	ageType m_Age;
};

//1. 类模板没有自动类型推导的使用方式
//2. 类模板在模板参数列表中可以有默认参数

void test02()
{
	1. 类模板没有自动类型推导的使用方式
	//Person2 p("李四",10);         错误:无法进行类型推导
	Person2<string, int>person("张三", 11);
	person.showInfo();

	//2. 类模板在模板参数列表中可以有默认参数
	Person2<string>person1("李四", 12);
	person1.showInfo();
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

to sum up:

  • Class templates can only be used to display the specified type

  • The template parameter list in the class template can have default parameters

3. When to create member functions in class templates

There is a difference in the creation timing of the member functions in the class template and the member functions in the ordinary class:

  • Member functions in ordinary classes can be created from the beginning

  • Member functions in the class template are created when they are called

#include <iostream>
#include <string>
using namespace std;


// 普通类中的成员函数一开始就可以创建
// 类模板中的成员函数在调用时才创建

class Person3
{
public:
	void showPerson3()
	{
		cout << "Person3 show" << endl;
	}
};

class Person33
{
public:
	void showPerson33()
	{
		cout << "Person33 show" << endl;
	}
};

template<class T>
class MyClass
{
public:
	T obj;
	
	//类模版中的成员函数
	void func1()
	{
		obj.showPerson3();
	}
	void func2()
	{
		obj.showPerson33();
	}
};


int main(void)
{
	test03();

	system("pause");
	return 0;
}

This can be compiled successfully.

In addition to the called function, an error will be reported, indicating that the member function in the class template is created when it is called

void test03()
{
	MyClass<Person3>p1;
	p1.func1();
	//p1.func2();   //编译会出错,说明函数调用才会去创建成员函数
}

4. Class template object as function parameter

There are three incoming methods:

     (1) Specify the incoming type --- directly display the data type of the object

     (2) Parameter templating --- Turn the parameters in the object into templates for transmission

     (3) The entire class is templated --- the object type is templated for delivery

 

(1) Specify the incoming type --- directly display the data type of the object

#include <iostream>
#include <string>
using namespace std;

//类模版
template<class T1,class T2>
class Person4
{
public:
	//构造函数
	Person4(T1 name,T2 age):m_Name(name),m_Age(age)
	{
	}
	//显示函数
	void showPerson()
	{
		cout << "姓名:" << this->m_Name << endl;
		cout << "年龄:" << this->m_Age << endl;
	}

	T1 m_Name;
	T2 m_Age;
};

//1、指定传入的类型
void printPerson1(Person4<string,int>&p)
{
	p.showPerson();
}
void test04()
{
	Person4<string, int>p("孙悟空", 123);
	printPerson1(p);
}

int main(void)
{
	test04();

	system("pause");
	return 0;
}

(2) Parameter templating --- Turn the parameters in the object into templates for transmission

#include <iostream>
#include <string>
using namespace std;

//类模版
template<class T1,class T2>
class Person4
{
public:
	//构造函数
	Person4(T1 name,T2 age):m_Name(name),m_Age(age)
	{
	}
	//显示函数
	void showPerson()
	{
		cout << "姓名:" << this->m_Name << endl;
		cout << "年龄:" << this->m_Age << endl;
	}

	T1 m_Name;
	T2 m_Age;
};


//2、参数模板化
template<class T1,class T2>
void printPerson004(Person4<T1, T2>&p)
{
	p.showPerson();
    cout << "T1的类型:" << typeid(T1).name() << endl;
	cout << "T2的类型:" << typeid(T2).name() << endl;
}
void test004()
{
	Person4<string, int>p("猪八戒", 123);
	printPerson004(p);

}

int main(void)
{
	test004();
	system("pause");
	return 0;
}

The string name is a bit long

class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >

(3) The entire class is templated --- the object type is templated for delivery

#include <iostream>
#include <string>
using namespace std;

//类模版
template<class T1,class T2>
class Person4
{
public:
	//构造函数
	Person4(T1 name,T2 age):m_Name(name),m_Age(age)
	{
	}
	//显示函数
	void showPerson()
	{
		cout << "姓名:" << this->m_Name << endl;
		cout << "年龄:" << this->m_Age << endl;
	}

	T1 m_Name;
	T2 m_Age;
};


//3、整个类模板化
template<class T>
void printPerson0004(T& p)
{
	p.showPerson();
	cout << "T的类型为:" << typeid(T).name() << endl;
}
void test0004()
{
	Person4<string, int>p("唐僧", 23);
	printPerson0004(p);
}
int main(void)
{
	test0004();
	system("pause");
	return 0;
}

to sum up:

  • Objects created by class templates can pass parameters to functions in three ways

  • The more widely used is the first one: specify the type of incoming

5. Class templates and inheritance

When the class template encounters inheritance, you need to pay attention to the following points:

  • When the parent class inherited by the subclass is a class template, when the subclass is declared, the type of T in the parent class should be specified

  • If not specified, the compiler cannot allocate memory to the subclass

  • If you want to flexibly specify the type of T in the parent class, the subclass also needs to become a class template

#include <iostream>
#include <string>
using namespace std;

//类模版与继承
template<class T>
class Base
{
public:
	T m;
};

class Son :public Base
{

};


int main(void)
{


	system("pause");
	return 0;
}

#include <iostream>
#include <string>
using namespace std;

//类模版与继承
template<class T>
class Base
{
public:
	T m;
};

//class Son :public Base              错误,必须要知道父类中T的类型
class Son:public Base<int>
{

};

void test05()
{
	Son son;
}

int main(void)
{


	system("pause");
	return 0;
}

If you want to flexibly specify the T type in the parent class, the subclass also needs to change the template

#include <iostream>
#include <string>
using namespace std;

//类模版与继承
template<class T>
class Base
{
public:
	T m;
};


//如果想灵活指定父类中的T类型,子类也需要变模版
template<class T1,class T2>
class Son05 :public Base<T2>
{
public:
    Son05()
	{
		cout << "T1的类型:" << typeid(T1).name() << endl;
		cout << "T2的类型:" << typeid(T2).name() << endl;
	}

	T1 s;
};

void test005()
{
	Son05<int, char>s2;
}

int main(void)
{
	test005();

	system("pause");
	return 0;
}

Summary: If the parent class is a class template, the subclass needs to specify the data type of T in the parent class

 

 

To be continued...

 

 

 

 

Guess you like

Origin blog.csdn.net/Zhouzi_heng/article/details/114845173