C + + template Class review

Source: Internet
Author: User

C + + template Class review
#include <iostream>
using namespace Std;


Template <class T1, class T2 = string>
Class Test
{
Private
T1 Temp1;
T2 Temp2;
Public
Test () {}
Test (T1 data1, T2 data2): Temp1 (data1), Temp2 (data2) {}
void Show ();
};


Template <class T1, class t2>//define Templates class members
void Test<t1, T2>::show ()
{
cout << "Primitive Class" << Endl;
cout << temp1 << endl << temp2 << Endl;
}


Template <>//display materialization
Class Test<int, int>
{
Private
int Temp1;
int temp2;
Public
Test () {}
Test (int one, int): Temp1 (one), Temp2 (both) {}
void Show ()
{
cout << "materialized" << Endl;
cout << temp1 << endl << temp2 << Endl;
}
};


Template <class t>
Class Test<t, double>
{
Private
T Temp1;
Double Temp2;
Public
Test () {}
Test (T data1, double data2): Temp1 (data1), Temp2 (data2) {}
void Show ()
{
cout << "partial materialization" << Endl;
cout << temp1 << endl << temp2 << Endl;
}
};


Template class Test<double, double>; The display materialization and display instantiation of a template class does not conflict because the display materialization does not create a class declaration


To use a template class as a built-in member
Template <class t1>
Class Test1
{
Private
Template <class t2>
Class Test2
{
Private
T2 data2;
Public
Test2 () {}
Test2 (T2 D):d Ata2 (d) {}
void Show2 ()
{
cout << data2 << Endl;
}
};
Test2<t1> data1;
Public
Test1 (T1 D):d ata1 (d) {}
void Show1 ()
{
Data1.show2 ();
}
};


Template <template <class T1, class t2> class te>
Class Test3
{
Private
Te<int, int> A;
Public
Test3 (Te<int, int> D): A (d) {}
void Show1 ()
{
A.show ();
}
};


int main ()
{
Test<int> A (1, "Linukey");//The second parameter will use the default argument string
A.show ();


Test<double, double> A (5, 6);//implicit instantiation
A.show ();


Test<int,int> A (5, 6);//Because of the int,int display materialization, the use of materialized classes
A.show ();


Test<int, double> A (1, 2.1);//Partial materialization
A.show ();


Test1<int> A (1); Template classes as built-in members of a class
A.show1 ();


Test<int, int> A (1, 2); Template class as a parameter
Test3<test> B (a);
B.show1 ();






return 0;
}

C + + template Class review

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.