C + + template (1) Introduction and use of methods __c++

Source: Internet
Author: User

A working template is a powerful tool in the C + + language that allows you to pass a data type as a parameter so that you do not need to write the same code for the same data type

For example, you sort different types of data. Instead of writing multiple duplicate codes, you will not write just one sort function and pass the data type as a parameter.

C + + uses two keywords to support templates:

Template<typename t>
Template<class t>

A specific example of use:

#include <iostream>
using namespace std;

Template<typename t>
t TMax (t arg1, T arg2) {return
	(Arg1 > Arg2)? arg1:arg2;


int main () {

	cout << tmax<int> (1, 2) << Endl;
	cout << tmax<float> (1.0, 2.5) << Endl;
	cout << tmax<char> (' G ', ' f ') << Endl;
	
	
	System ("PAUSE");
	return 0;
}



With the use of the template:

Class templates allow you to define your own type in a class, or you can have a system-self-contained type

Template<typename t>
class Array {
private:
	T *ptr;  A pointer to store the data
	int size;

Public:
	Array (T array[], int s);  Constructor
	void print ();

The definition of a function in a class must first be declared template

template<typename t>
array<t>::array (T array[], int s)
{
	ptr = new T[s];  Initialize capacity
	size = s;

	for (int i = 0; i < size; ++i)
		ptr[i] = Array[i];
}

Template<typename t>
void Array<t>::p rint ()
{for
	(int i = 0; i < size; ++i) {
		cout ;< *ptr<< "";
		ptr = ptr + 1;
	}
	cout << Endl;
}

The use of the following methods:

int main () {

	int arr[5] = {1,2,3,4,5};
	
	Array<int> Int_array (arr, 5);
	Int_array.print ();

	System ("PAUSE");
	return 0;
}


The template can have multiple parameters, such as:

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

Template<typename T1, TypeName t2>
class Multitemplatetest {
private:
	T1 arg1;
	T2 arg2;

Public:
	multitemplatetest (T1 a1,t2 a2) {
		arg1 = A1;
		arg2 = A2;
	}

	inline void print () {
		cout << arg1 << Endl;
		cout << arg2 << endl;
	}
;


int main () {

	Multitemplatetest<char, float> multitest (' a ', 1.2); \
	multitest.print ();

	System ("PAUSE");
	return 0;
}

6. Function overload and function template difference function overloading and templates are polymorphic features in OOP.
A function overload is an operation in which multiple functions do something similar, while a template function is one in which multiple functions do the same.

the template expands during compilation, similar to the macro definition. The difference is that the compiler carries out type security checks before the template is expanded. The principle is simple, the source code contains only the template function/template class, but the code generated by the compilation may contain multiple copies of the same function/class.










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.