CPP templates: Template inheritance skills

Source: Internet
Author: User
// 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;
}

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.