static 函数 、const 函数小结(实战篇)
来源:互联网 发布:策略为王 源码下载 编辑:程序博客网 时间:2024/06/06 07:12
static 函数小结
#include<iostream>#include<stdio.h>using namespace std;class point { public: void show(void) //普通方法可以访问访问static data { cout << "x = "<< x <<endl; cout << "y = "<< y<<endl; cout<< " flag = "<<flag<<endl; } public: point() {} point(int x,int y):x(x),y(y) {} point (point &sd) { x = sd.x; y = sd.y; flag = sd.flag; } point &operator = (point &sd) { if(this != &sd) { x= sd.x; y = sd.y; flag = sd.flag; } return *this; } ~point() {} private: int x; int y; static int flag; //静态数据,他不属于某一个对象的,因为他封闭了this指针 //所有对象共享一份数据,};int point :: flag = 100; //静态数据成员这样初始化,注:必须初始化int main(){ point a(1,3); a.show(); return 0;}运行结果:x = 1y = 3 flag = 100Press any key to continue
1.static 成员数据 ,可以由普通方法来访问
2.static 必须初始化,并且初始化不同于普通变量初始化。
3.static 函数(封锁了this 指针) 只能访问static data 数据。
#include<iostream>#include<stdio.h>using namespace std;class point { public:/* void show(void) //普通方法可以访问访问static data { cout << "x = "<< x <<endl; cout << "y = "<< y<<endl; cout<< " flag = "<<flag<<endl; }*/ static void show(void ) { cout << "flag = "<<flag<<endl; } public: point() {} point(int x,int y):x(x),y(y) {} point (point &sd) { x = sd.x; y = sd.y; flag = sd.flag; } point &operator = (point &sd) { if(this != &sd) { x= sd.x; y = sd.y; flag = sd.flag; } return *this; } ~point() {} private: int x; int y; static int flag; //静态数据,他不属于某一个对象的,因为他封闭了this指针 //所有对象共享一份数据,};int point :: flag = 100; //静态数据成员这样初始化,注:必须初始化int main(){ point a(1,3); a.show(); point ::show(); return 0;}运行结果:flag = 100flag = 100Press any key to continue
两种调用方式:
1 . 类名::static 函数
2 . 实例化一个对象.进行调用
#include<iostream>using namespace std;class Student{public: Student(int n,float s):num(n),score(s){} void change() const { cout << "num = "<<num++ <<endl; //表示对象里的数据不能改变 } void display() const { cout<<num<<"\t"<<score<<endl; } private: const int num; const float score;} ;int main(){ Student const stud(101,78.5); stud.display(); stud.change(); stud.display(); return 0;};Compiling...u.cppD:\VC++\Microsoft Visual Studio\MyProjects\uuu\u.cpp(66) : error C2166: l-value specifies **const object**执行 cl.exe 时出错.
#include<iostream>using namespace std;class Student{public: Student(int n,float s):num(n),score(s){} void change(int n,float s) const { num=n; score=s; } void display() const { cout<<num<<"\t"<<score<<endl; } private: mutable int num; mutable float score;} ;int main(){ Student const stud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return 0;};101 78.5101 80.5Press any key to continue
0 0
- static 函数 、const 函数小结(实战篇)
- const/static 成员函数
- 实战 oracle 函数小结
- C++中的友元函数、static函数、常函数小结(理论篇)
- 函数后面的const,static函数
- const成员函数与static成员函数
- C++ const成员函数小结
- C++重载函数的实现小结(实战篇)
- C中的static函数小结
- static,const小结
- static与const小结
- static与const小结
- const and static(小结)
- const、static、extern小结
- Static成员函数不能修饰const
- 为什么static 成员函数不能为const
- 二级指针、函数指针、枚举、static、const
- static 和 const的解释(用 const 限定类的成员函数)
- Java中的排序
- NOI2016 滚粗记
- 杭电5505GT and numbers(判断一个数通过累乘他的因子是否可以得到另一个数)
- Django集成的子框架
- Android应用与MVP模式
- static 函数 、const 函数小结(实战篇)
- 插入排序算法之移动交换法
- 第一章.对象导论之 java与Internet
- centos下的MySQL安装(rpm方式)
- plsql developer登陆失败问题
- 常用Redis命令总结
- python中的json解析
- Smallest Difference poj2718
- java api封装与工厂模式