C + + Learning notes (3)----static members of class templates

Source: Internet
Author: User

As with any other class, a class template can declare a static member:

Template <typename t>classFoo { Public:    Staticstd::size_t count () {returnCtr; }    voidaddctr () {Ctr++; }    voidsubctr () {Ctr--; }Private:    Staticstd::size_t Ctr; //The following can have other member interfaces};template<typename t>std::size_t Foo&LT;T&GT;::CTR =0;//Define and Initialize Ctr

In this code, Foo is a template class that has a public static member function named Count and a private static data member called Ctr. Each instance of Foo has its own static member instance. That is, for any given type X, there is a foo<x>::ctr and a foo<x>::count () member function. All objects of the foo<x> type share the same Ctr object and the Count function. For example:

// instantiating static members Foo<string>::ctr and Foo<string>::count foo<string> fs; // all three objects share the same foo<int>::ctr and Foo<int>::count members foo<int> Fi, Fi2, Fi3;

As with any other static data member, each static data member of the template class must have and have only one definition. However, each instance of a class template has a unique static object. So, like the member function that defines a template, we also define the static data member as a template:

Template <typename t>std::size_t Foo<t>::ctr=0;    // Define and Initialize Ctr

Like any other member of the class template, the first part of the definition is the parameter list of the template, followed by the type and name of the member we define. As always, the member name includes the class name of the member, and for the class generated from the template, the class name includes the template argument. Therefore, when you instantiate Foo with a specific template parameter type, a separate CTR is instantiated for that class type and initialized to 0.

As with static members of non-template classes, we can access the static members of a class template through a class type object, or you can access members directly using the scope operator. Of course, in order to access static members directly through a class, we must refer to a specific instance.

To run the example:

#include <iostream>#include<vector>#include<string>using namespacestd;template<typename t>classFoo { Public:    Staticstd::size_t count () {returnCtr; }    voidaddctr () {Ctr++; }    voidsubctr () {Ctr--; }Private:    Staticstd::size_t Ctr; //The following can have other member interfaces};template<typename t>std::size_t Foo&LT;T&GT;::CTR =0;//Define and Initialize CtrintMainintargcChar*argv[]) {Foo<int>fi; cout<<"fi.count () ="<< Fi.count () <<Endl;    Fi.addctr (); Foo<int>Fi2;    Fi2.addctr (); cout<<"fi2.count () ="<< Fi2.count () <<Endl; cout<<"fi.count () ="<< Fi.count () <<Endl; cout<<"foo<int>::count () ="<< foo<int>::count () <<Endl; Foo<Double>FD; cout<<"fd.count () ="<< Fd.count () <<Endl; return 0;}

Operation Result:

s:\computertech\vs2015\zhangsir_proj\console\console\debug>template.exefi. Count () =0  fi2.count ()=2fi. Count () =2Foo<int>::count () = 2 fd.count ()=0

Like any other member function, a static member function is instantiated only when it is used.

C + + Learning notes (3)----static members of class templates

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.