About static members of C ++ class

Source: Internet
Author: User

The values of static data members are the same for each object. It is a headache for professional programmers to implement static member functions, after reading this article, you will surely be able to solve the problem of static member functions that are your headache!

In the implementation of static member functions, non-static members described in the class cannot be directly referenced. Static members described in the class can be referenced. If a non-static member is to be referenced in a static member function, it can be referenced through an object. The following is an example to illustrate this.

 
 
  1. # Include
  2. Class M
  3. {
  4. Public:
  5. M (int ){A=A; B + = ;}
  6. Static void f1 (M m );
  7. Private:
  8. Int;
  9. Static int B;
  10. };
  11.  
  12. Void M: f1 (M m)
  13. {
  14. Cout<<"A="<< 
  15. Cout<<"B="<< 
  16. }
  17.  
  18. IntM: B=0;
  19. Void main ()
  20. {
  21. M p (5), Q (10 );
  22. M: f1 (P); file: // The object name is not used during the call.
  23. M: f1 (Q );
  24. }

You can analyze the results by yourself. It can be seen that the following format is used to call a static member function: static member functions of a class cannot be called by default like non-static member functions (because static member functions do not have the implicit this parameter ).

In a static member function of a class, as long as a pointer to an object of this type is obtained in some way, and there is a suitable access level, this object can call its non-static member functions.

1. Used to save the number of objects.
2. As a flag, Mark whether some actions occur, such as the file opening status, printer usage status, and so on.
3. Store the memory address of the first or last member of the linked list.

Static member functions are basically equivalent to a global function with a namespace.
1. You can directly use this function without generating an object instance. For example, Cxxx: MyStaticFunc ();
2. The window callback function must use a static member function or a global function.
3. static member functions or global functions are required for thread calls.

C ++ has developed against the historical background of the popular C language. At the beginning and later, C ++ was a superset of C, all C features are included in C ++, which leads to a large number of subsequent C ++ introductory books starting with C. In fact, this is misleading, although C ++ is a superset of C, the major significance of using abstract mechanisms to expand C language is to use abstraction to cover the various language features exposed in C, it allows programmers to program on a more natural abstraction layer.

For example, you do not use int * to add an array size n to represent an array, but use a Automatically increasing vector. For example, you do not use malloc/free, instead, you can use smart pointers and RAII technology to manage resources. For example, instead of adding a group of functions to an exposed class with a struct containing only data, you can use the real ADT.

For example, you do not use the returned values of second-class to express errors, but use the first-class language-level exception mechanism. However, C is the source of C ++ after all, and the underlying layer is still C when C ++ is stripped off. What's more important is that in actual coding, sometimes it does need "C ++", for example, in the module-level binary interface encapsulation. Bjarne also said that abstract mechanisms such as OO/GP are only suitable for use in appropriate places.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

Related Article

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.