C++——static & const
静态成员
由关键字static修饰说明的类成员,称为静态类成员(static class member)。虽然使用static修饰说明,但与函数中的静态变量有明显差异。类的静态成员为其所有对象共享,不管有多少对象,静态成员只有一份存于公用的内存中。
静态成员又分为静态成员函数,静态成员数据
静态数据
#include<iostream>
using namespace std; class Test
{
friend void fun(Test &t);
public:
Test(int d=)
{
count++;
data = d;
}
~Test()
{
count--;
}
private:
int data;
static int count;
}; int Test::count = ; void fun(Test &t)
{
cout << "t.data = " << t.data << endl;
cout << "Object count = " << Test::count << endl;
} int main()
{
Test t1();
Test t2();
fun(t1);
}
静态成员数据在类外初始化
静态函数
#include<iostream>
using namespace std; class Test
{
friend void fun(Test &t);
public:
Test(int d=)
{
count++;
data = d;
}
~Test()
{
count--;
}
void show1()
{
cout << "data = " << data << endl;
cout << "Object count = " << Test::count << endl;
}
static void show2()
{
cout << "Object count = " << Test::count << endl;
}
private:
int data;
static int count;
}; int Test::count = ; void fun(Test &t)
{
cout << "t.data = " << t.data << endl;
cout << "Object count = " << Test::count << endl;
} int main()
{
Test t1();
Test t2();
fun(t1);
}
静态方法只能访问静态变量,不能访问普通变量
普通方法技能访问静态变量,也能访问普通变量
为啥会这样呢?
对于类的普通成员函数,该函数必须经由一个对象去激活(有一个this指针)。
但是这条规则对于static修饰的成员函数(静态方法)不适用,静态方法没有this指针。我们代码里面访问data,实际上事以this->data这种方式,而静态函数连this都没有,他怎么能访问data呢?
#include<iostream>
using namespace std; class Test
{
friend void fun(Test &t);
public:
Test(int d=)
{
count++;
data = d;
}
~Test()
{
count--;
}
void show1()
{
cout << "data = " << data << endl;
cout << "Object count = " << Test::count << endl;
}
static void show2()
{
cout << "Object count = " << Test::count << endl;
}
void show3()
{
show1();
show2();
}
static void show4()
{
show2();
} private:
int data;
static int count;
}; int Test::count = ; void fun(Test &t)
{
cout << "t.data = " << t.data << endl;
cout << "Object count = " << Test::count << endl;
} int main()
{
Test t1();
Test t2();
fun(t1);
}
静态方法只能访问静态方法,不能访问普通方法
普通方法技能访问静态方法,也能访问普通方法
原因同上
const方法
#include<iostream>
using namespace std; class Test
{
friend void fun(Test &t);
public:
Test(int d=)
{
count++;
data = d;
}
~Test()
{
count--;
}
//void change(Test * const this) 导致this为常量
void change()
{
data--;
cout << endl;
}
//void change(Test * const this)const 演变成
//void change(const Test * const this) 导致*this 和 this都是常量
void change()const
{
//data--;
cout << endl;
} private:
int data;
static int count;
}; int Test::count = ; void fun(Test &t)
{
cout << "t.data = " << t.data << endl;
cout << "Object count = " << Test::count << endl;
} int main()
{
Test t1();
Test t2();
fun(t1);
}
const常方法限定该函数不能修改类的数据成员
#include<iostream>
using namespace std; class Test
{
friend void fun(Test &t);
public:
Test(int d=)
{
count++;
data = d;
}
~Test()
{
count--;
}
void show1()
{
cout << endl;
}
void show2() const
{
cout << endl;
}
//void change1(Test * const this) 导致this为常量
void change1()
{
show1();
show2();
}
//void change2(Test * const this)const 演变成
//void change2(const Test * const this) 导致*this 和 this都是常量
void change2()const
{
//show1();
show2();
} private:
int data;
static int count;
}; int Test::count = ; void fun(Test &t)
{
cout << "t.data = " << t.data << endl;
cout << "Object count = " << Test::count << endl;
} int main()
{
Test t1();
Test t2();
fun(t1);
}
普通方法能够调用常方法
常方法不能调用普通方法
C++——static & const的更多相关文章
- static、const和static const
http://blog.csdn.net/rainkin1993/article/details/8068558 #include<iostream> using namespace st ...
- static 类成员变量 和 static const类成员变量
1.使用static类的优点: (1)避免与其他类的成员或者全局变量冲突 (2)可以封装 (3)阅读性好 2.static 数据成员独立于该类的任意对象而存在 static数据成员的类型可以是该成员所 ...
- static const vs. extern const
在实现文件(.m文件)中使用static const来定义“只在编译单元内可见的常量”(只在.m文件内可见),由于此类常量不在全局符号表中,所以无须为其名称加类名前缀(一般以k开头). 在头文件中使用 ...
- iOS—— static和const联合使用;使用static const 与 #define
static和const联合使用: static将一个全局变量变成局部变量 const将一个局部变量变成局部常量 // 定义了一个局部常量 static const CGFloat ...
- Static Const
Static 内部的 Const 不可变的 一般写法 在.m文件中, static NSString *const ID = @"shop"; static const CGFlo ...
- (转) C++ static、const和static const 以及它们的初始化
const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. static表示的是静态的.类的静态成员函数.静态成员变量是和类相关的,而不是和类的 ...
- C++ static、const和static const 以及它们的初始化
转自C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. s ...
- 类内const static(static const)成员变量初始化问题
在查找const相关资料的过程中,又遇到了另外一个问题,就是C++类中const static(或者static const)成员变量应当如何初始化的问题. 查阅了许多资料,发现VC环境下,只允许co ...
- How to initialize a static const map in c++?
#include <map> using namespace std; struct A{ static map<int,int> create_map() { map< ...
- static const readonly
C#中的static 和Java中的static 简单,两者用法完全是一致的.从两方面讨论: 1. 变量是属于类的,不是实例级别的.只能通过类名调用,不能通过实例调用. 2. 如果在定义时就赋值了,那 ...
随机推荐
- Python - Django - 模板语言之 Filters(过滤器)
通过管道符 "|" 来使用过滤器,{{ value|过滤器:参数 }} Django 的模板语言中提供了六十个左右的内置过滤器 urls.py: from django.conf. ...
- gen语言
概率编程语言(PPL)领域正经历着机器学习技术快速发展带来的奇迹般的复兴.在短短的几年里,PPL 已经从一个模糊的统计研究领域发展出十几个活跃的开源方案.最近,麻省理工学院(MIT)的研究人员推出了一 ...
- 实现不同的项目,用不同的git 账号提交
可以全局配置一个git 账户名和密码,然后在具体项目里单独配置一个账户名和密码 例如: git config --global user.name "winyh" git conf ...
- yaml中使用harbor
1.在harbor的ui界面上注册一个账号 姓名:zihao 全名:zhuzihao 密码:Zihao@5tgb 邮箱:15613691030@163.com 2.在需要下载镜像的机器上,同样需要修改 ...
- 如何解决mac brew遇到无法下载的依赖?
使用brew安装软件时,需要下载依赖包,但是如果依赖包特别大的时候,就很容易失败:brew本身不支持断点续传:但是可以使用wget -c的方式断点续传的下载依赖:下载完如何给brew安装使用呢?参考: ...
- AWS 架构最佳实践概述(十一)
AWS 架构最佳实践 AWS合理架构的框架支柱 安全性 - 保护并监控系统 能够保护信息.系统和资产 通过风险评估和缓解策略 可靠性 - 从故障中恢复并减少中断 从基础设施或服务故障中恢复 动态获取计 ...
- MySQL语句增加字段,修改字段名,修改类型,修改默认值
原文地址:https://blog.csdn.net/kimgoo/article/details/54630257 增加字段:alter table 表名 ADD 字段 类型 约束 [默认值 注释] ...
- 最新 医渡云java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.医渡云等10家互联网公司的校招Offer,因为某些自身原因最终选择了医渡云.6.7月主要是做系统复习.项目复盘.LeetCo ...
- InstallerProjects打包
C#—使用InstallerProjects打包桌面应用程序 前言 打包桌面应用程序实在是一个不常使用的东西,偶尔使用起来经常会忘东忘西的耽误时间,因此,这篇文章多以图片记录过程,也是用于备忘. ...
- python基础篇(二)
PYTHON基础篇(二) if:else,缩进 A:if的基础格式和缩进 B:循环判断 C:range()函数和len()函数 D:break,contiue和pass语句 for,while循环 函 ...