C + + class template three (static keyword in class templates)

Source: Internet
Author: User

//the static keyword in the class template#include <iostream>using namespacestd;/*The  class template is essentially a C + + compiler that creates different classes based on the type parameters, C + + The compiler generates a static variable for each class when the class is generated using the class template then it is very good for the static keyword in the class to understand that the static keyword-decorated variable is a static static variable of an object-shared class belonging to the same class of the class The variable data type of the static decoration in a class template must be determined not to be a type parameter because a static variable is initialized before the class object the class class is not generated by the class template*/Template<typename t>classpoint{ Public: Point (T a,t b) { This->x =A;  This->y =b; }    Static intTotal ; Static voidPrint () {cout<<"total="<< Total <<Endl; }Private: T x; T y;};//the only way to initialize static variables in a classTemplate<typename t>intPoint<t>::total =Ten;voidProtecta () { point<int> P1 (3,3); P1.total= A; Point<int> P2 (5,5); Point<float> P3 (4.0,4.0); P3.total= -; //share a static variable with the same classP2.    Print (); //different classes do not share a static variablepoint<float>::P rint ();}voidMain () {protecta (); System ("Pause");}

C + + class template three (static keyword in 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.