Detailed description of the number of C ++ statistical objects

Source: Internet
Author: User

The C ++ programming language supports a variety of programming styles and meets the needs of developers to a great extent. Here we will introduce the implementation of the number of objects in C ++ statistics. I hope my friends can get some help.

Use auto_ptr to count the number of objects in C ++. Be sure to declare the constructor as private. The code for counting the number of objects in C ++ is as follows:

 
 
  1. class A{  
  2. private:  
  3. static int i;  
  4. A(){  
  5. }  
  6. A(const A&){}  
  7. public:  
  8. static void init(){  
  9. A::i=0;  
  10. }  
  11. auto_ptr<A> static makeA(){  
  12. i++;  
  13. auto_ptr<A> p(new A);  
  14. return p;  
  15. }  
  16. static int getNum(){  
  17. return A::i;  
  18. }  
  19. };  
  20. int A::i=0;  
  21. int main(){  
  22. for(int i=0;i<10;i++){  
  23. auto_ptr<A> p=A::makeA();  
  24. }  
  25. cout<<A::getNum()<<endl;  

The Operation Method for counting the number of objects in C ++ is described here.

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.