【c++】静态属性和静态方法

#include<iostream>
#include<string>
//基类
class Pet
{
public:
	Pet(std::string theName);
	~Pet();

	static int getCount();//作为一个接口
protected:
	std::string name;
private:
	static int count;
};
//子类
class Dog : Pet
{
public:
	Dog( std::string theName );
};
//子类
class Cat : Pet
{
public:
	Cat( std::string theName );
};

int Pet::count = 0;	//分配内存,初始化为0

Pet::Pet(std::string theName)
{
	name = theName;
	count++;

	std::cout<<"一只宠物出生了,名字叫做:"<<name<<"\n";
}

Pet::~Pet()
{
	count--;
	std::cout<<name<<"挂掉了\n";
}

int Pet::getCount()
{
	return count;
}

Cat::Cat(std::string theName):Pet(theName)
{
}

Dog::Dog(std::string theName):Pet(theName)
{
}

int main()
{
	Dog dog("Tom");
	Cat cat("Jerry");

	std::cout<<"已经诞生了"<<Pet::getCount()<<"只宠物\n\n";

	{
		Dog dog_2("Tom_2");
		Cat cat_2("Jerry_2");

		std::cout<<"已经诞生了"<<Pet::getCount()<<"只宠物\n\n";
	}

	std::cout<<"\n现在还剩"<<Pet::getCount()<<"宠物\n\n";

	return 0;
}

静态方法的调用应使用:className::memethodName();

不要使用:objectName::methodName()会让代码变得很糟糕


静态成员是所有对象共享的,不能在静态方法里访问非静态的元素

非静态方法可以访问类的静态成员,也可以访问类的非静态成员




举个例子:

类A的声明与实现如下:

[cpp]  view plain  copy
  1. #pragma once  
  2. class A  
  3. {  
  4.     public:  
  5.         int count1;  
  6.         //static int count2=100;//error ,带有类内初始值设定项的成员必须为常量  
  7.         //const static int count3=100;//正确   
  8.         static int count2;  
  9.     public:  
  10.         int getCount1();  
  11.         static int getCount2();  
  12.   
  13.     public:  
  14.         A(void);  
  15.         ~A(void);  
  16. };  

[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2. #include "A.h"  
  3.   
  4.   
  5. A::A(void):count1(99)  
  6. {  
  7. }  
  8.   
  9.   
  10. A::~A(void)  
  11. {  
  12. }  
  13.   
  14. int A::getCount1()  
  15. {  
  16.     return count1;  
  17. };  
  18.   
  19. //static int A::getCount2()//error,此处不能指定存储类,即类体外来实现静态成员函数,不能加static关键字  
  20. //{  
  21. //  
  22. //};  
  23.   
  24. int A::getCount2()  
  25. {  
  26.     return count2*1000;  
  27. };  
  28.   
  29. //初始化  
  30. int A::count2(1000);  


测试文件如下:


[cpp]  view plain  copy
  1. // staticTest.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include "A.h"  
  7.   
  8. using namespace std;  
  9.   
  10. int _tmain(int argc, _TCHAR* argv[])  
  11. {  
  12.     A *a=new A;  
  13.     //访问非静态成员  
  14.     cout<<a->count1<<endl;     
  15.     cout<<a->getCount1()<<endl;  
  16.       
  17.     cout<<"------------------"<<endl;     
  18.   
  19.     //访问静态成员  
  20.     cout<<a->count2<<endl;     
  21.     cout<<(*a).count2<<endl;  
  22.     cout<<A::count2<<endl;    
  23.       
  24.     cout<<a->getCount2()<<endl;    
  25.     cout<<(*a).getCount2()<<endl;  
  26.     cout<<A::getCount2()<<endl;   
  27.   
  28.     delete(a);  
  29.   
  30.     return 0;  
  31. }  



打印结果:



注意事项:


1.静态数据成员不能在类中初始化,实际上类定义只是在描述对象的蓝图,在其中指定初值是不允许的。也不能在类的构造函数中初始化该成员,因为静态数据成员为类的各个对象共享,否则每次创建一个类的对象则静态数据成员都要被重新初始化。静态成员的值对所有的对象是一样的。静态成员可以被初始化,但只能在类体外进行初始化(通常在实现文件中进行初始化)。


2.静态成员函数在类外实现时候无须加static关键字,否则是错误的。


3.静态成员仍然遵循public,private,protected访问准则。


4.静态成员函数没有this指针,它不能返回非静态成员,因为除了对象会调用它外,类本身也可以调用。静态成员函数可以直接访问该类的静态数据和函数成员,而访问非静态数据成员必须通过参数传递的方式得到一个对象名,然后通过对象名来访问。


5.静态成员之间可以相互访问,包括静态成员函数访问静态数据成员和访问静态成员函数;非静态成员函数可以任意地访问静态成员函数和静态数据成员;静态成员函数不能访问非静态成员函数和非静态数据成员;调用静态成员函数,可以用成员访问操作符(.)和(->)为一个类的对象或指向类对象的指针调用静态成员函数;静态成员变量只能被静态成员函数调用,静态成员函数也是由同一类中的所有对象共用,只能调用静态成员变量和静态成员函数。


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值