zoukankan      html  css  js  c++  java
  • C++类的静态成员_11

    一。成员变量的回顾

      1.通过对象名能够访问 public 成员变量。

      2.每个对象都可以有只属于自己的成员变量

      3.成员变量不能在对象之间共享。

    二。类的静态成员

      1. C++中可以定义静态成员变量和静态成员函数

      2. 静态成员属于整个类所有,不需要依赖任何对象。

      3. 可以通过类名直接访问public 静态成员

      4. 可以通过对象名访问public静态成员

      5. 静态成员函数可以直接访问静态成员变量。

    三。静态成员变量的定义

      1. 在定义时直接通过 static 关键字修饰

      2. 静态成员变量不依赖于任何对象,需要在类外单独分配空间。

      3. 语法规则:Type ClassName::VarName:

    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    class Test
    {
      private:
            static int ci;       
      public:  
            static int GetI()
            {
                return ci;    
            }
            static void SetI(int i)
            {
                  ci = i;  
            }
    };
    int Test::ci = 0;
    
    int main(int argc, char *argv[])
    {
        Test::SetI(5);
        cout<<"Test::ci = " << Test::GetI() << "
    ";
        
        cout << "Press the enter key to continue ...";
        cin.get();
        return EXIT_SUCCESS;
    

      5.从命名空间的角度

        a。类的静态成员只是类这个命名空间中的全局变量和全局函数

        b。不同的是,类可以对静态成员进行访问权限的限制,而命名空间不行

      6.从面向对象的角度

        a。类的静态成员属于类概念本身

        b。类的所有对象共享相同的静态成员

    四。静态成员函数的定义

      1.在定义时直接通过 static 关键字修饰

      2.其余部分与普通成员函数定义相同

    五。静态成员函数和普通成员函数有什么区别?

      c++类对象中的成员变量和成员函数是分开存储的

      1.成员变量

        普通成员变量:存储于对象中,与 struct 变量有相同的内存布局和字节对齐方式

        静态成员变量:存储于全局数据区中

      2.成员函数

        存储于代码段中

    六。

    #include <stdio.h>
    
    class Test
    {
        int i;
        int j;
        int k;
        
        static int c;
    public:
        Test(int i, int j, int k)
        {
            this->i = i;
            this->j = j;
            this->k = k;
        }
        
        void print()
        {
            printf("Object Address: %08X
    ", this);
            printf("&c = %08X, c = %d
    ", &c, c);
            printf("&i = %08X, i = %d
    ", &i, i);
            printf("&j = %08X, j = %d
    ", &j, j);
            printf("&k = %08X, k = %d
    ", &k, k);
        }
    };
    
    int Test::c;
    
    int main()
    {
        Test t1(0, 1, 2);
        Test t2(3, 4, 5);
        
        printf("t1 Address: %08X
    ", &t1);
        t1.print();
        printf("t2 Address: %08X
    ", &t2);
        t2.print();
        
        printf("Press any key to continue...");
        getchar();
        return 0;
    }

    六。小结

      1.C++类中可以包含属于类概念的静态成员

      2.静态成员变量在全局数据区分配空间

      3.静态成员函数不包含隐藏的this指针

      4.通过类名可以直接访问静态成员

  • 相关阅读:
    Codeforces 1291 Round #616 (Div. 2) B
    总结
    刷新DNS解析缓存+追踪+域名解析命令
    数学--数论--Hdu 5793 A Boring Question (打表+逆元)
    Lucene.net(4.8.0) 学习问题记录六:Lucene 的索引系统和搜索过程分析
    LeetCode 117 Populating Next Right Pointers in Each Node II
    LeetCode 116 Populating Next Right Pointers in Each Node
    test test
    LeetCode 115 Distinct Subsequences
    LeetCode 114. Flatten Binary Tree to Linked List
  • 原文地址:https://www.cnblogs.com/lvxiaoning/p/7598062.html
Copyright © 2011-2022 走看看