// Bolgcontent. cpp: defines the console application Program .
//
# Include "stdafx. H"
# Include <iostream>
# Include <string>
Using namespace STD;
/* Example of class template inheritance
* 1. inherit template parameters
*/
Template <typename base, int D>
Class discriminator: public base {};
/* 2. Recursive template Mode
* The derived class uses itself as a template parameter to pass to the base class. The simplest situation is as follows:
*/
Template <typename derived>
Class criousbase {
//......
};
Class crious: Public criousbase <crious> {};
Template <typename T>
Class crioustemplate: Public criousbase <crioustemplate <t>
{};
// A simple application of crpt is to record the total number of classes constructed
// Templatetest. cpp: defines the entry point of the console application.
//
# Include "stdafx. H"
# Include <iostream>
# Include <typeinfo>
# Include <string>
Using namespace STD;
Template <typename countedtype>
Class objectcounter {
PRIVATE:
Static size_t count;
Protected:
// Default constructor
Objectcounter (){
++ Objectcounter <countedtype>: count;
}
// Copy the constructor
Objectcounter (objectcounter const &){
++ Objectcounter <countedtype>: count;
}
// Destructor
~ Objectcounter (){
-- Objectcounter <countedtype>: count;
};
Public:
// Return the number of existing objects
Static size_t live (){
Return objectcounter <countedtype >:: count;
}
};
Template <typename countedtype>
Size_t objectcounter <countedtype>: Count = 0;
Template <typename T>
Class countt: Public objectcounter <countt <t>
{
Public:
Countt ()
{}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
Countt <int> tt;
Cout <TT. Live () <Endl;
Getchar ();
Return 0;
}
// III. parameterized Virtualization
Class notvirtual {
};
Class virtual {
Public:
Virtual void Foo (){
}
};
Template <typename vbase>
Class base: Private vbase {
Public:
// The virtual nature of Foo () depends on the Declaration in its base class vbase.
Void Foo (){
STD: cout <"base: Foo ()" <Endl;
}
};
Template <typename v>
Class derived: public base <v> {
Public:
Void Foo (){
STD: cout <"derived: Foo ()" <Endl;
}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
Base <notvirtual> * P1 = new derived <notvirtual>;
P1-> Foo (); // call base: Foo ()
Base <virtual> * P2 = new derived <virtual>;
P2-> Foo (); // call derived: Foo ()
Return 0;
}