Static members of a class

Source: Internet
Author: User

question: How do you count the number of objects? (1) Common member variables? No, because for normal member variables, the object has a member variable of the child, they are independent of each other, so it is not possible to achieve the count of the number of objects
(2) Global variables: Yes, but generally speaking, we try to avoid global variables (long periods, memory, error, difficult to find),
(3) static member variables and static member functions for a class: YES
 Static Members:(1) A static member is owned by the entire class and does not depend on any object, depending on the existence of the class(2) Static members of public can be accessed through the class name
(3) The static members of public can be accessed through the object name .
(4) Static members are not assigned within a class because they are shared by all objects of that class, and are generally initialized outside the class;
 static member variables:    In fact, it is directly connected to the static keyword can be, static member variables do not depend on any object, you need to allocate space outside the class, in fact, is stored in the Global data area (static storage area) Static member functions:is the function name plus the static keyword, remember, do not initialize static member variables within a class
  1. class A
  2. {
  3. private:
  4. staticint i;// 静态成员变量
  5. public:
  6. staticint geti()// 静态成员函数
  7. {
  8. return i;
  9. }
  10. void seti(int v)
  11. {
  12. i = v;
  13. }
  14. };
  15. // 不要在类内进行初始化,因为静态成员变量是所有的对象的共有的,
  16. int A::i =10;// 静态成员变量进行初始化
  17. int main()
  18. {
  19. A aa;
  20. aa.seti(10); // 静态成员变量是private,所以只能被类内的函数访问
  21. printf("i = %d\n", aa.geti());// 通过对象进行访问
  22. printf("i = %d\n", A::geti());// 通过类名访问
  23. while(1);
It can be understood that a static member of a class is a global variable inside a namespace within a class, that is, today the member is a global variable within the class,
 statistics on the number of objects implemented:
  1. class A
  2. {
  3. private:
  4. staticint count;
  5. public:
  6. A()// 构造函数
  7. {
  8. count++;
  9. }
  10. ~A()// 析构函数
  11. {
  12. count--;
  13. }
  14. };
  15. int A::count =0;// 静态成员变量进行初始化
every time the object is created, count will be added one, the object is destroyed, it is reduced by one, so it completes the realization of the number of objects;static member variables are initialized, usually in the outside of the class, if not their own initialization, is actually the value static member variable default initial value is zero, cause analysis: Because the static member variable is stored in the static storage area (Global storage area) of all the non-initialized values, The compiler automatically assigns them a value of zero.
  the difference between a static member function and a normal function(1) The difference between the static keywords,
(2) A static member function is actually a global function within a class, not dependent on the object, but rather on the class
The normal member function is dependent on an object within the class,
(3) A static member function can only access static member variables in a class, and normal member functions are
(4) A static member function does not contain a pointer to a specific object, and the normal member function contains a pointer to the
 Note: The This pointer is used by the compiler to automatically pass the address of the object itself to a function within the class, passed to this. This pointer is scoped to the inside of the class, remember that the this pointer is only in the ordinary member function of the class, the static member function is absolutely no this pointer, Allocation of storage space:
  1. class A
  2. {
  3. private:
  4. int i;
  5. int j;
  6. short a;
  7. short b;
  8. };
  9. class B
  10. {
  11. private:
  12. int i;
  13. int j;
  14. short a;
  15. short b;
  16. staticint c;
  17. };
  18. class C
  19. {
  20. private:
  21. int i;
  22. int j;
  23. short a;
  24. short b;
  25. staticint c;// 全局数据区(静态存储区)
  26. public:
  27. C()// 函数的部分是栈空间
  28. {
  29. }
  30. ~C()
  31. {
  32. }
  33. };
  34. int C::c =10;
three classes, the size of the allocated space, is 12 bytes, because the static member variable is not allocated within the object, but is allocated in the global data area (static storage area), and the function whether it is static is in the code snippet,  From for notes (Wiz)



Static members of a class

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.