C ++ Templates Chapter 1 Templates and inheritance examples

Source: Internet
Author: User

[Cpp]
# Include <iostream>
 
Using namespace std;
 
// Intermediate Template
Template <typename Base, int D>
Class Discrim: public Base
{
Public:
Discrim (){
Cout <"Discrim ()" <endl;
}

};
 
// The PolicySelector of the Discrim class can be inherited from the same base class multiple times.
Template <typename set1, typename set2, typename set3, typename set4>
Class PolicySelector: public Discrim <set1, 1>, public Discrim <set2, 2>, public Discrim <set3, 3>, public Discrim <set4, 4>
{
};
 
Class DefaultPol1 {
Public:
Void print (){
Cout <"p1 print ()" <endl;
}
};
 
Class DefaultPol2 {
Public:
Void print (){
Cout <"p2 print ()" <endl;
}
};
 
Class DefaultPol3 {
Public:
Void print (){
Cout <"p3 print ()" <endl;
}
};
 
Class DefaultPol4 {
Public:
Void print (){
Cout <"p4 print ()" <endl;
}
};
 
// Create a set of default values
Class DefaultPols
{
Public:
Typedef DefaultPol1 dp1;
Typedef DefaultPol2 dp2;
Typedef DefaultPol3 dp3;
Typedef DefaultPol4 dp4;
};
 
Class DefaultPolArgs: virtual public DefaultPols {};
 
// Write several templates to overwrite the Default policy parameters.
// 1
Template <typename Policy>
Class policypolicis: virtual public DefaultPols
{
Public:
Typedef Policy dp1;

Virtual void print (){
Dp1 dd;
Dd. print ();
}
};
 
// 2
Template <typename Policy>
Class Policy2_is: virtual public DefaultPols
{
Public:
Typedef Policy dp2;

Virtual void print (){
Dp1 dd;
Dd. print ();
}
};
 
// 3
Template <typename Policy>
Class Policy3_is: virtual public DefaultPols
{
Public:
Typedef Policy dp3;

Policy3_is (){
Cout <"Policy3_is ()" <endl;
}

Virtual void print (){
Dp1 dd;
Dd. print ();
}
};
 
// 4
Template <typename Policy>
Class Policy4_is: virtual public DefaultPols
{
Public:
Typedef Policy dp4;

Virtual void print (){
Dp1 dd;
Dd. print ();
}
};
 
// All the default values here are the same
Template <typename PolicySet1 = DefaultPolArgs,
Typename PolicySet2 = DefaultPolArgs,
Typename PolicySet3 = DefaultPolArgs,
Typename PolicySet4 = DefaultPolArgs>
Class BreadSlicer
{
Public:
// Create a selector. PolicySelector uses the template parameters to initialize them as the corresponding type.
// In this way, the parent class of PolicySelector is specified, and it is inherited from its own template parameters...
// The understanding of this part is not clear yet ??
Typedef PolicySelector <PolicySet1, PolicySet2, PolicySet3, PolicySet4> policies;

Void print (){
Typename policies: dp1 p1;
P1.print ();
 
Typename policies: dp2 p2;
P2.print ();

Typename policies: dp3 p3;
P3.print ();

Typename policies: dp4 p4;
P4.print ();
}

};
 
Class CustomPoliA
{
Public:
Void print (){
Cout <"CustomPoliA print ()" <endl;
}

};
 
Class CustomPoliB
{
Public:
Void print (){
Cout <"CustomPoliB print ()" <endl;
}

};
 
Int main (int argc, char ** argv)
{
BreadSlicer <Policy3_is <CustomPoliA>, Policy4_is <CustomPoliB> bc;
Bc. print ();

Return 0;
}
 
 
Starting:/home/Robert tkun/projects/tevent/build/tevent
P1 print ()
P2 print ()
CustomPoliA print ()
CustomPoliB print ()
* ** Exited normally ***

Related Article

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.