- 1、本文档共9页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用。所以在所有对象中都可以共享它。使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节省内存。
静态成员的定义或声明要加个关键static。静态成员可以通过双冒号来使用即类名::静态成员名。
在C++中类的静态成员变量和静态成员函数是个容易出错的地方,本文先通过几个例子来总结静态成员变量和成员函数使用规则,再给出一个实例来加深印象。希望阅读本文可以使读者对类的静态成员变量和成员函数有更为深刻的认识。
第一个例子,通过类名调用静态成员函数和非静态成员函数
[cpp] \o view plain view plain \o copy copy \o print print \o ? ?
class Point
{
public:
void init()
{
}
static void output()
{
}
};
void main()
{
Point::init();
Point::output();
}
编译出错:error C2352: Point::init : illegal call of non-static member function
结论1:不能通过类名来调用类的非静态成员函数。
第二个例子,通过类的对象调用静态成员函数和非静态成员函数
将上例的main()改为:
[cpp] \o view plain view plain \o copy copy \o print print \o ? ?
void main()
{
Point pt;
pt.init();
pt.output();
}
编译通过。
结论2:类的对象可以使用静态成员函数和非静态成员函数。
第三个例子,在类的静态成员函数中使用类的非静态成员
[cpp] \o view plain view plain \o copy copy \o print print \o ? ?
#include stdio.h
class Point
{
public:
void init()
{
}
static void output()
{
printf(%d\n, m_x);
}
private:
int m_x;
};
void main()
{
Point pt;
pt.output();
}
编译出错:error C2597: illegal reference to data member Point::m_x in a static member function
因为静态成员函数属于整个类,在类实例化对象之前就已经分配空间了,而类的非静态成员必须在类实例化对象后才有内存空间,所以这个调用就出错了,就好比没有声明一个变量却提前使用它一样。
结论3:静态成员函数中不能引用非静态成员。
第四个例子,在类的非静态成员函数中使用类的静态成员
[cpp] \o view plain view plain \o copy copy \o print print \o ? ?
class Point
{
public:
void init()
{
output();
}
static void output()
{
}
};
void main()
{
Point pt;
pt.output();
}
编译通过。
结论4:类的非静态成员函数可以调用用静态成员函数,但反之不能。
第五个例子,使用类的静态成员变量
[cpp] \o view plain view plain \o copy copy \o print print \o ? ?
#include stdio.h
class Point
{
public:
Point()
{
m_nPointCount++;
}
~Point()
{
m_nPointCount--;
}
static void output()
{
printf(%d\n, m_nPointCount);
}
private:
static int m_nPointCount;
};
void main()
{
Point pt;
pt.output();
}
按Ctrl+F7编译无错误,按F7生成EXE程序时报链接错误
error LNK2001: unresolved external symbol private: static int Point::m_nPointCount (?m_nPointCoun
您可能关注的文档
- _______教学质量监控体系.docx
- 0-龙源电力安全生产管理通则-(修订).docx
- 人教版新版一年级上册道德与法制课件.docx
- 人教版新版一年级上册道德与法制.docx
- 会计计算题目13个.docx
- 房屋检测与加固.docx
- 高中古诗词默写.docx
- 流热仿真课后作业.doc
- 职称计算机考试Internet冲刺试题5.docx
- 银兴疾控发[]00号(兴庆区基本公共卫生服务项目第二季度督导情况报告).doc
- 北师大版必修第二册第1章1周期变化课件(21张)(1).pptx
- 18.基本功强化训练(二) 有理数的乘除运算.pptx
- Unit5Whattimeisit?(课件)-湘少版英语三年级下册.pptx
- 北京市首都师范大学附属高二上学期期中考试英语试题 解析版.doc
- Unit5Let'seatPartALet'slearnLet'sdo(课件)人教PEP版英语三年级上册(1).pptx
- 1.3氧化还原反应第一课时课件高一上学期化学人教版.pptx
- Unit3Lesson(1)3Let’sBuyPostcards(课件)冀教版英语五年级下册.pptx
- 山东省栖霞市第一中学高中英语必修五课件module4Carnival(共20张).ppt
- 北师大版必修三UNIT8GREENLIVINGTOPICTALKLESSON1课件-12张.pptx
- 北师大版必修二UNIT6THEADMIRABLE(LESSON3—READINGCLUB2)词汇课件(25张).pptx
最近下载
- 小学:音乐人音版三年级下册第一单元《爱祖国》大单元设计.docx
- 华润饮料(1991.HK)新股纵览:包装纯净饮用水龙头企业,锐意进取止于至善.pdf
- 办文办会办事流程图.doc
- 常山北明:拟处置资产涉及的闲置报废机器设备资产评估报告.docx VIP
- 昭和电工正式量产 VGCF-等级纳米碳管-VGCF-项目介绍.docx
- 表面改性剂祥解.ppt
- 教学课件:第2讲《办好中国的事情关键在党》.pptx VIP
- (最新)24年秋统编版四年级语文上册习作:我的家人【教案及反思】.docx
- 安川骏马系列.pdf
- 初中地理综合实践作业设计方案-七年级(七)绘制学校平面图-人教版七年级上册.docx VIP
文档评论(0)