C++--第11课 - 类的静态成员

第11课 - 类的静态成员

1. 成员变量的回顾

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

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

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

 

2. 新的需求

统计类在程序运行期间有多少个对象同时存在;保证程序的安全性,并随时可以获取对象的数目。

 

3. 类的静态成员

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

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

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

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

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

l  静态成员变量的定义

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

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

语法规则:Type ClassName::VarName。

l  静态成员函数的定义

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

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

l  示例:

 

例子:

#include <stdio.h>

class Test

{

private:

    static int cI;

public:

    static int GetI()

    {

        return cI;

    }

    static void SetI(int i)

    {

        cI = i;

    }

    void print()

    {

        printf("cI = %d\n", cI);

    }

};

int Test::cI = 0;

int main()

{

    Test::SetI(5); 

    printf("Test::cI = %d\n", Test::GetI());

    Test t1;

    Test t2;

    t1.print();

    t2.print();

    t1.SetI(10); 

    t2.print();  

    printf("Test::cI = %d\n", Test::GetI());  

    printf("Press any key to continue...");

    getchar();

    return 0;

}

运行结果:

Test::cI = 5

cI = 5

cI = 5

cI = 10

Test::cI = 10

l  从命名空间的角度

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

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

l  从面向对象的角度

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

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

 

2. 静态成员的应用—统计某个类的对象数目

#include <stdio.h>

class Test

{

private:

    static int cCount;

public:

    static int GetCount()

    {

        return cCount;

    }

    Test()

    {

        cCount++;

    }  

    ~Test()

    {

        cCount--;

    }

};

int Test::cCount;  //cCount在没有初始化的时候,静态全局变量的初始值是0。因为全//局变量和静态变量的默认初始值都是0。

void run()

{

    Test ta[100]; 

    printf("Number of Object: %d\n", Test::GetCount());

}

int main()

{

    Test t1;

    Test t2;

    printf("Number of Object: %d\n", Test::GetCount());

    run();    //run返回之后它其中的局部变量就会被销毁。

    printf("Number of Object: %d\n", Test::GetCount());

    printf("Press any key to continue...");

    getchar();

    return 0;

}

运行结果:

Number of Object: 2

Number of Object: 102

Number of Object: 2

 

问题:静态成员函数和普通成员函数有什么区别?

 

3. C++对象模型初步探索

struct C1

{

int i;

int j;

short k;

short l;

};

class C2

{

int i;

int j;

short k;

short l;

};

struct C3

{

int i;

int j;

short k;

short l;

static int c;  //定义在静态空间上,不依赖任何对象的存在。

public:

C3()

{

}

void print()

{

}

};

sizeof(C1) = sizeof(C2) = sizeof(C3) = 12

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

成员变量:

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

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

成员函数:

存储于代码段中。

C++中的class从面向对象理论出发,将变量和函数集中定义在一起,用于描述现实世界中的类。从计算机的角度,程序依然由数据段和代码段构成。

 

4. C++编译器如何完成面向对象理论到计算机程序的转化

class Test

{

private:

int ml;

public:

Test(int i)

{

ml =i;

}

int getI()

{

return mI;

}

static void Print()

{

printf(“This is class Test.\n”);

}

};

Test a(10);

a.getI();

Test::Print();

上面的程序经过C++编译器的内部处理变为:

struct Test

{

int mI;

};

void Test_initialize(Test* pThis, int i)

{

pThis->mI = i;

}

int Test_getI(Test* pThis)

{

return pThis->mI;

}

void Test_Print()

{

printf(“This is class Test.\n”);

}

Test a;

Test_initialize(&a, 10);

Test_getI(&a);

Test_Print();

 

5. 静态成员函数与普通成员函数的区别

静态成员函数不包含指向具体对象的指针。

普通成员函数包含一个指向具体对象的指针。

C++中类的普通成员函数都隐式包含一个指向当前对象的this指针。

6. 程序—静态成员与非静态成员的区别

#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\n", this);

        printf("&c = %08X, c = %d\n", &c, c);

        printf("&i = %08X, i = %d\n", &i, i);

        printf("&j = %08X, j = %d\n", &j, j);

        printf("&k = %08X, k = %d\n", &k, k);

    }

};

int Test::c;

int main()

{

    Test t1(0, 1, 2);

    Test t2(3, 4, 5);

    printf("t1 Address: %08X\n", &t1);

    t1.print();

    printf("t2 Address: %08X\n", &t2);

    t2.print();

    printf("Press any key to continue...");

    getchar();

    return 0;

}

 

 

小结:

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

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

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

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

通过对象名可以访问静态成员,所有的对象可以共享同一个类的静态成员。

 

posted @ 2019-08-11 19:10  free-锻炼身体  阅读(390)  评论(0编辑  收藏  举报