Membership Template (member template)

Source: Internet
Author: User
Tags int size

1. About the member functions of the class template

A member function of a class template defined outside a class has the following form:

[1] must begin with the keyword template, followed by the template parameter list of the class

[2] You must indicate which class the member is

[3] class names must contain their template parameters


2. Membership Template (member template)

Any class (template or non-template) can have itself a member of a class template or function template, which is called a member template. The member template cannot be virtual.

[1] Nested class templates

[2] member function templates

When you define a member template within the scope of a class template, you must include two template parameter lists.

Add:

A nested class (nested Class) is a separate class, essentially unrelated to its outer class. Therefore, the objects of the perimeter class and the nested class are independent of each other. The objects of a nested type do not have the members defined by the outer class, and the members of the outer class do not have the members defined by the nested class.



Here is an example of a member function template.

Consider that two stack assignments are assigned to each other, which means one of the whole is assigned to the other.

You cannot assign a type of stack to another type of stack, even if the two types can be implicitly transformed:

Stack<int> IntStack1, IntStack2; 
Stack<float> Floatstack; 
 ... 
IntStack1 = IntStack2;  OK
floatstack = intStack1;  ERROR

As long as you define the assignment operator as a template, you can make the stacks of the two "types different, but whose elements can be implicitly transformed" to each other.

</pre><pre name= "code" class= "CPP" > #include <deque> #include <stdexcept> using namespace std; Template<typename t> class stack{private:deque<t> elems; public:void push (const t& x) {Elems.push_bac K (x);
		void Pop () {if (empty ()) throw Out_of_range ("STATK&LT;&GT;::p op (): Empty stack");
		
	Elems.pop_back ();
		} T Top () const {if (empty ()) throw Out_of_range ("Statk<>::top (): Empty stack"); 
	return Elems.back ();
	int size () const {return elems.size ();}
	BOOL Empty () const {return elems.empty ();  }//member function template//defines an inner layer (inner) template parameter T2 in a template that owns template parameter T template<typename
T2> stack<t> & operator= (const stack<t2>& RHS);

}; Template<typename t> template<typename t2> stack<t>& stack<t>::operator= (const Stack<

	t2>& RHS) {if (void*) This = = (void*) &AMP;RHS)//Judge whether to assign value to oneself return *this;	Stack<t2> temp (RHS);	There will be construction overhead elems.clear ();
		while (!temp.empty ()) {Elems.push_front (Temp.top ());
	Temp.pop ();
return *this;
 }





Main reference materials:

[1] C + + primer,4rd

[2] C + + Templates--the Complete Guide

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.