(32.1) static member

1. Static member

  • You can define a static member of the class, be able to share data between multiple objects of the same kind.
  • The advantages of using a static member of the class are:
    ① name of the static member is in the scope of the class, thus avoiding name conflicts with other members of the class or global object;
    ② static members can implement the package, can be a private members, and the global must not.
    ③ static member is associated with a particular class, a clear structure.
  • eg:
class employee { //雇员类定义
	private:
	int empNo;
	int id;
	char *name;
	……
};
  • Static data members is a special class data members, which begin with the keyword static, the statement in the form of:
class 类名 
{ //类体
	static 数据成员类型 数据成员名列表; //静态数据成员声明
};

eg:count设计的目的是计数Data类总共有多少个实例化对象。
class Data//Data类定义
{
	public:
		static int count;//静态数据成员
		int maxlevel;//非静态公有数据成员
		Data(int i=0){...,count++;}//构造函数
	private:
		int level;//非静态私有数据成员
};
int Data::count=0;//静态数据成员定义且初始化

说明:
实例化对象,就会给每个对象分配存储空间,也就给该对象的每个数据成员分配存储空间
count属于类,不属于对象,一个类只有一个static数据成员
  • Static data members to store schematic
    Here Insert Picture Description
  • Description of the static data members:
1) 通常, 非静态数据成员存在于类类型的每个对象中, 静态数据成员则独立于该类的任何对象, 在所有对象之外单独开辟空间存储。 
在为对象所分配的空间中不包括静态数据成员所占的空间。
2) 如果只声明了类而未定义对象, 则类的非静态数据成员是不占存储空间的, 只有在定义对象时, 才为对象的数据成员分配空间。
但是只要在类中定义了静态数据成员, 即使不定义任何对象, 也为静态数据成员分配空间, 它可以在尚未建立对象时就被引用。
3) 访问静态成员时同样需要遵守公有及私有访问规则。
4) 静态数据成员必须在类外部定义一次(仅有一次) , 静态成员不能通过类构造函数进行初始化, 而是在类外定义时进行初始化。
定义静态数据成员的方式为:
数据成员类型 类名::静态数据成员名=初始化式;
5)静态数据成员可用作默认实参, 非静态数据成员不能用作默认实参, 因为它的值不能独立于所属的对象而使用。 
例如:
class Data 
{ //Data类定义
	Data& setbkcolor(int=bkcolor);
	static const int bkcolor = 5;
};
6)有了静态数据成员(相当于全局变量), 各对象之间实现了数据共享, 因此可以不使用全局变量。
  • eg
class test
{
	private:
		int x;
		int y;
	public:
		static int num;
		static int Getnum()
		{
			x+=5;
			num+=15;
			return num;
		}
};

int test::num=10;

int main(void)
{
	test a;
	cout<<test::num<<endl;   //输出10
	test::num=20;
	cout<<test::num<<endl;   //输出20
	cout<<test::Getnum()<<endl;  //输出35
	cout<<a.Getnum()<<endl;//输出50
	return 0;
}

2. static member function

  • Member function can also be defined as static, plus static in front of the class function declaration became the general form of a static member function, declared as follows:
class 类名 
{ //类体
	static 返回类型 函数名(类型1 参数名1,类型2 参数名2,);
};

eg:
static int getcount() 
{ 
	return count; 
} //静态成员函数
  • Two ways of calling a static member function mode
    and static data members, static member function is part of a class, rather than part of the object.
    If you want to call public static member functions outside the class, can class scope operator (: :) and static member functions by calling the object name,
    for example:
cout<<Data::getcount()<<'\t'<<d.getcount();
  • The fundamental difference between static and non-static member functions member functions are: non-static member function pointers have this, but this is not a static member function pointers.
    Therefore, the static member functions of this class can not access non-static members. Static member function is specifically designed to access static data members.
  • Static member function can not be declared as const.
  • eg:
#include <iostream>
using namespace std;
class CTest
{
	public:
		CTest()
		{
			s_total++;
			id=s_total;
			cout<<"构造"<<id<<" ";
		}
		~CTest()
		{
			s_total--;
			cout<<"析构"<<id<<" ";
		}
		static int gettotal()
		{
			return s_total;//静态成员函数可以访问静态成员
		}
	private:
		static int s_total;
		int id;

};

int CTest::s_total=0;
int main(void)
{
	CTest a,b,c;//调用三次了构造函数
	CTest *p=new CTest;//调用一次了构造函数
	cout<<"合计="<<CTest::gettotal()<<" ";//输出4
	delete p;
	cout<<"合计="<<CTest::gettotal()<<" ";//输出3
	return 0;
}

  • eg: compiler error, because the non-static member function init can not pass the class name to call the class.
class Test
{
	void init(){}
	static void output()
	{}
};

int main(void)
{
	Test::init();//错的
	Test::output();//对的
	return 0;
}
  • eg: compiler, object class can use a static member functions and non-static member functions.
class Test
{
	public:
		void init() { }
		static void output() { }
}

int main()
{
	Test t;
	t.init();
	t.output();
	return 0;
}
  • eg: compiler error, because the static member function can not reference non-static members.
    Static member function belongs to the class as a whole has been allocated a space before the class to instantiate an object, rather than a static member of the class must have memory space after the class to instantiate an object
class Test
{
	public:
		void init() { }
		static void output()
		{
			cout<<x<<endl;
		}
	private:
		int x;		
};

int main()
{
	Test t;
	t.output();
	return 0;
}
  • eg: compiler, since the class of non-static member function can be called with a static member function, but not vice versa.
class Test
{
	public:
		void init()
		{
			output();
		}
		static void output()
		{
			
		}
};

int main()
{
	Test t;
	t.output();
	return 0;
}
  • eg: link error, because static data members of the class must be initialized before use.
    If we add before the main function int Point :: m = 0;
    then compile and link error, outputs an operation program.
class Test
{
	public:
		Test()
		{
			m++;//正确
		}
		~Test()
		{
			m--;//正确
		}
		static void output()
		{
			cout<<m<<endl;
		}
	private:
		static int m;
}

int main()
{	
	Test t;
	t.output();
	return 0;
}


Published 510 original articles · won praise 134 · Views 150,000 +

Guess you like

Origin blog.csdn.net/u011436427/article/details/103949219