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. 如果在定义时就赋值了,那 ...
随机推荐
- Bmp格式图片与16进制的互相转换简解 Python
BMP TO HEX 首先介绍Github上一个简单的Bmp转成16进制的py: https://github.com/robertgallup/bmp2hex 网上这种例子很多.思路也简单:将bmp ...
- [ kvm ] 学习笔记 4:KVM 高级功能详解
1. 半虚拟化驱动 1.1 virtio 概述 KVM 是必须使用硬件虚拟化辅助技术(如 Intel VT-x .AMD-V)的 Hypervisor,在CPU 运行效率方面有硬件支持,其效率是比较高 ...
- linux中查看端口号
linux中查看端口号 yum install lsof -y [root@test1 ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF ...
- jQuery 控制網頁捲軸移動 & Ignore link '#' method jump action
$('a.gotoheader').click(function(){ // 讓捲軸移動到 0 的位置 $(); // ignore link "#" method return ...
- 正则表达式入门教程&&经典Javascript正则表达式
前言 例子: ^.+@.+\\..+$ 这样的代码曾经多次把我自己给吓退过.可能很多人也是被这样的代码给吓跑的吧.继续阅读本文将让你也可以自由应用这样的代码. 正文 教程:正则表达式30分钟入门教程 ...
- 基于TreeSoft实现mysql、oracle、sql server的数据同步
一.为了解决数据同步汇聚,数据分发,数据转换,数据维护需求,TreeSoft推出了数据同步,数据处理等丰富功能 . TreeSoft作为中间传输载体负责连接各种数据源,为各种异构数据库之间架起沟通的桥 ...
- Http 请求到后端过程
描述下网页一个 Http 请求,到后端的整个请求过程 评注:基础题,感觉属于常识题!必会! 回答: 利用DNS进行域名解析 -------> 发起TCP的3次握手 --------> 建立 ...
- How to change default root@ email address linux / postfix / centos?
Change root@hostname to different email address By default, any email sent by system is sent to root ...
- 用jquery和php实现ajax异步请求响应
ajax技术可以实现异步请求和响应,下面的是用jquery向一个php脚本发送异步请求,并得到响应. 第一步,准备好前台的html表单,和jquery实现的ajax请求 <html lang=& ...
- 简单layer 快速上手
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...