C++深度解析(16)—类的静态成员

1.类成员的回顾 

  • 从面向对象的角度 
    • 对象由属性(成员变量)和方法(成员函数)构成 
  • 从程序运行的角度 
    • 对象由数据和函数构成 
    • 数据可以位于栈,堆和全局数据区 
    • 函数只能位于代码段
  • 结论 
    • 通过对象名能够访问public成员变量
    • 每个对象都可以有只属于自己的成员变量
    • 成员变量不能在对象之间共享

2.新的需求

  • 统计在程序运行期间某个类的对象数目
  • 保证程序的安全性(不能使用全局变量) 
  • 随时可以获取当前对象的数目

3.编程实验

  • 解决方案的尝试  
#include <stdio.h>  

class Test
{
private:
	int mCount;

public:
	Test() : mCount(0)
	{
		mCount++;
	}
	
	~Test()
	{
		--mCount;
	}
	
	int getCount()
	{
		return mCount;
	}
};

Test gTest;

int main()
{
	Test t1;
	Test t2;

	printf("count = %d\n", gTest.getCount());
	printf("count = %d\n", t1.getCount());
	printf("count = %d\n", t2.getCount());

	getchar();
	
	return 0;
}
  • 运行结果:

4.类的静态成员

  • 在C++中可以定义静态成员变量和静态成员函数
  • 静态成员属于整个类所有,不需要依赖任何对象
  • 可以通过类名直接访问public静态成员
  • 可以通过对象名访问public静态成员
  • 静态成员函数只能操作类的静态成员,无法操作类的非静态成员

5 静态成员定义

  • 静态成员变量定义
    • 在定义时直接通过static关键字修饰
    • 静态成员变量不依赖于任何对象,需要在类外单独分配空间,存储在全局数据区,任然受private和public的限制
    • 语法规则:Type ClassName::VarName;
  • 静态成员函数的定义
    • 在定义时直接通过static关键字修饰
    • 其余部分与普通成员函数定义相同
#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);   // 通过类名可以直接访问puiblic静态成员函数
	
	printf("Test::cI = %d\n", Test::GetI());

	Test t1; 
	Test t2;
    
	t1.print();    // 静态成员属于整个类共享 
	t2.print();
    
	t1.SetI(10); // 通过对象名访问public静态成员函数
	t2.print();
    
	printf("Test::cI = %d\n", Test::GetI());
    
	printf("Press any key to continue...");
    
	getchar();
    
	return 0;
}
  • 运行结果:
  • 从命名空间的角度
    • 类的静态成员只是类这个命名空间中的全局变量和全局函数
    • 不同之处只是,类可以对静态成员进行访问权限的限制,而命名空间不行
  • 从面向对象的角度
    • 类的静态成员属于类概念本身
    • 类的所有对象共享相同的静态成员

6.静态成员的应用

  • 统计某个类的对象数目
#include <stdio.h>

class Test
{
private:
	static int cCount;

public:
	static int GetCount()
	{
		return cCount;
	}
    
	Test()
	{
		cCount++;
	}
    
	~Test()
	{
		cCount--;
	}
};

int Test::cCount;  // 默认为0,和全局变量和静态局部变量相同

void run()
{
    Test ta[100];
    
    printf("Number of Object: %d\n", Test::GetCount());
}

int main()
{
	Test t1;
	Test t2;   // t1.t2在main()函数返回时被销毁
    
	printf("Number of Object: %d\n", Test::GetCount());
    
	run();  // ta[100]在run返回时被销毁
    
	printf("Number of Object: %d\n", Test::GetCount());
    
	printf("Press any key to continue...");
   
	getchar();
    
	return 0;
}
  • 运行结果:

7.c++对象模型

  • C++类对象中的成员变量和成员函数是分开存储的,某一个类的对象中只有成员变量(普通成员变量),无成员函数 
    • 成员变量
      • 普通成员变量:存储于对象中,与struct变量有相同的内存布局和字节对齐方式
      • 静态成员变量:存储于全局数据区中
    • 成员函数:存储于代码段中
#include <stdio.h>

struct C1
{
	int i;
	int j;
	short k;
	short l;
};

class C2
{
	int i;
	int j;
	short k;
	short l;
};

class C3
{
	int i;
	int j;
	short k;
	short l;

	static int c;

public:
	C3()
	{
	
	}

	void print()
	{
	
	}
};

int C3::c;
  
int main()
{
	C1 c1;
	C2 c2;
	C3 c3;
	
	printf("sizeof <c1>: %d\n", sizeof(c1));  // 12
	printf("sizeof <c2>: %d\n", sizeof(c2));   // 12
	printf("sizeof <c3>: %d\n", sizeof(c3));  // 12

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

	getchar();

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

8.c++编译器的内部处理

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

9.静态成员与非静态成员的区别

#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;   // C++中类的普通成员函数都隐式包含一个指向当前对象的this指针
		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指针。

10.小结

  • C++类中可以包含属于类概念的静态成员
  • 静态成员变量在全局数据区分配空间
  • 静态成员函数不包含隐藏的this指针
  • 通过类名可以直接访问静态成员
  • 通过对象名可以访问静态成员,所有的对象可以共享同一个类的静态成员
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值