您的位置:首页 > 编程语言 > C语言/C++

C++学习:static静态成员即static成员函数

2017-06-03 16:01 399 查看
摘要:

在学习java的时候,有一个非常重要的关键字static,静态的意思.在java中使用static修饰的变量或者函数,表示此变量是属于类的而不是属于对象的,在程序加载的时候就会初始化.

在C语言中使用static修饰的变量其声明周期被放大到了整个程序,使用static修饰的函数,表示其是内部函数,只能在内部进行使用.

而在C++中使用static修饰的变量或者函数表示的意思与java中类似,表示的是这个变量属于类,而并不是属于对象.使用static修饰的函数,可以直接使用(类.函数名)的形式来进行调用

友情提示

本文作者:章飞_906285288

本文作者博客地址:ttp://blog.csdn.net/qq_29924041

static的使用:

使用static来修饰变量:

使用static修饰的静态成员是在类外分配空间和初始化
如:
class Test{
private:
static int count; //定义一个类的静态成员
}
实现(注意必须要在类的外部进行定义):
int Test::count = 0;


使用static来修饰成员函数

使用static关键字来修饰成员函数:

class Test{
private:
static int count;
public:
static int getCount();
}

静态成员函数的实现(不需要写static):
int Test::getCount(){
return count;
}

静态成员函数的使用:
int count = Test::getCount();


static数据成员的使用注意事项:

在static定义的成员函数中是不能使用this的

即使没有实例化类的对象,static的数据成员和成员函数同样可以进行使用

static成员的名字在类的作用域中,因此可以避免与其它类的成员或者全局对象的名字冲突

可以实现封装,static成员可以是私有成员,但是全局对象不可以

通过阅读代码可以清晰的看到static修饰的成员是与类进行相关联的.

static的使用实例:

/*
* ===========================================================================
*
*       Filename:  static.h
*    Description:
*        Version:  1.0
*        Created:  2017年06月03日 13时25分41秒
*       Revision:  none
*       Compiler:  gcc
*         Author:   (),
*        Company:
*
* ===========================================================================
*/

#ifndef __STATIC_H__
#define __STATIC_H__

#ifdef __cplusplus
extern "C"{
#endif

class Student{
private:
int student_id;
//声明静态成员
static int student_count;
int student_age;
public:
Student(int student_id,int student_age);
~Student();
void study();
void read();
void write();
//声明静态成员函数
static int get_student_count();
int get_student_id();
int get_student_age();
void print_student_info();
protected:

};
#ifdef __cplusplus
}
#endif

#endif


/*
* ===========================================================================
*
*       Filename:  static.cpp
*    Description:
*        Version:  1.0
*        Created:  2017年06月03日 13时30分07秒
*       Revision:  none
*       Compiler:  gcc
*         Author:   (),
*        Company:
*
* ===========================================================================
*/

#include<iostream>
#include"static.h"
using namespace::std;

/**
* 在类外去初始化静态变量
*/
int Student::student_count = 0;

Student::Student(int id = 0,int age = 0):
student_id(id),student_age(age)
{
student_count++;
cout<<"student constructor"<<endl;
}

Student::~Student(){
student_count--;
cout<<"student reference distructor"<<endl;
}

void Student::study(){
cout<<"student study"<<endl;
}

void Student::read(){
cout<<"student read"<<endl;
}

void Student::write(){
cout<<"student write"<<endl;
}

int Student::get_student_id(){
return this->student_id;
}

int Student::get_student_age(){
return this->student_age;
}

int Student::get_student_count(){
return student_count;
}

void Student::print_student_info(){
cout<<"student_id:"<<student_id<<endl;
cout<<"student_age:"<<student_age<<endl;
cout<<"stduent_count:"<<student_count<<endl;
}


/*
* ===========================================================================
*
*       Filename:  staticTest.cpp
*    Description:
*        Version:  1.0
*        Created:  2017年06月03日 15时41分47秒
*       Revision:  none
*       Compiler:  gcc
*         Author:   (),
*
*
* ===========================================================================
*/
#include<iostream>
#include<static.h>
using namespace::std;

void fun(){
//函数执行的作用域是在两个括号之间
cout<<"================"<<endl;
Student stu_three(2,10);
cout<<"count:"<<Student::get_student_count()<<endl;
stu_three.print_student_info();
}

int main(int argc,char *argv[]){
//静态成员函数是直接可以使用类名来调用的
int count = Student::get_student_count();
cout<<"count:"<< count <<endl;
cout<<"------------------"<<endl;
Student stu(0,8);
cout<<"count:"<<Student::get_student_count()<<endl;
stu.print_student_info();
cout<<"------------------"<<endl;
Student stu_two(1,15);
cout<<"count:"<<Student::get_student_count()<<endl;
stu.print_student_info();
cout<<"------------------"<<endl;
fun();
cout<<"count:"<<Student::get_student_count()<<endl;
return 0;
}


以上是一个简单的小demo,

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息