静态成员

由关键字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的更多相关文章

  1. static、const和static const

    http://blog.csdn.net/rainkin1993/article/details/8068558 #include<iostream> using namespace st ...

  2. static 类成员变量 和 static const类成员变量

    1.使用static类的优点: (1)避免与其他类的成员或者全局变量冲突 (2)可以封装 (3)阅读性好 2.static 数据成员独立于该类的任意对象而存在 static数据成员的类型可以是该成员所 ...

  3. static const vs. extern const

    在实现文件(.m文件)中使用static const来定义“只在编译单元内可见的常量”(只在.m文件内可见),由于此类常量不在全局符号表中,所以无须为其名称加类名前缀(一般以k开头). 在头文件中使用 ...

  4. iOS—— static和const联合使用;使用static const 与 #define

    static和const联合使用:   static将一个全局变量变成局部变量   const将一个局部变量变成局部常量 // 定义了一个局部常量      static const CGFloat ...

  5. Static Const

    Static 内部的 Const 不可变的 一般写法 在.m文件中, static NSString *const ID = @"shop"; static const CGFlo ...

  6. (转) C++ static、const和static const 以及它们的初始化

    const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. static表示的是静态的.类的静态成员函数.静态成员变量是和类相关的,而不是和类的 ...

  7. C++ static、const和static const 以及它们的初始化

    转自C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. s ...

  8. 类内const static(static const)成员变量初始化问题

    在查找const相关资料的过程中,又遇到了另外一个问题,就是C++类中const static(或者static const)成员变量应当如何初始化的问题. 查阅了许多资料,发现VC环境下,只允许co ...

  9. How to initialize a static const map in c++?

    #include <map> using namespace std; struct A{ static map<int,int> create_map() { map< ...

  10. static const readonly

    C#中的static 和Java中的static 简单,两者用法完全是一致的.从两方面讨论: 1. 变量是属于类的,不是实例级别的.只能通过类名调用,不能通过实例调用. 2. 如果在定义时就赋值了,那 ...

随机推荐

  1. SignalR 传Model类型的参数

    目录 集线器方法 js调用 集线器方法 集线器写了一个方法是这样的 public void test(string name, Customer customer) 第一个参数是string类型的,第 ...

  2. Configuration system failed to initialize

    引用:https://cloud.tencent.com/developer/article/1336954 重装.net Framework

  3. ubuntu18.04下安装gitlab

    1.安装并配置必要的依赖关系 sudo apt-get update sudo apt-get install -y curl openssh-server ca-certificates 接下来,安 ...

  4. JVM(二) 栈内存结构

    栈内存是描述java方法执行的内存模型,每个方法在执行的同时都会创建一个栈帧(Stack Frame)用于存储局部变量表.操作数栈.动态链接.返回出口等信息.每一个方法从调用直至执行完成的过程,就对应 ...

  5. Linux chomd命令

    file 语法为: chmod abc file 其中a,b,c各为一个数字,分别表示User.Group.及Other的权限. r=4,w=2,x=1 若要rwx属性则4+2+1=7: 若要rw-属 ...

  6. [转帖]MySQL的又一神器-锁,MySQL面试必备

    MySQL的又一神器-锁,MySQL面试必备 https://segmentfault.com/a/1190000020762791 lock 低一级的是 latch   原文链接:blog.ouya ...

  7. Python21之内嵌函数和闭包

    一.内嵌函数 内嵌函数指的是在一个函数体内部定义的函数,可以称它为函数的函数,也就是子函数,外部的函数称之为母函数,就类似局部变量和全局变量 子函数体内定义的变量只在其函数内部有效,但其内部可以调用母 ...

  8. Redis--hash类型操作命令

    哈希类型hash redis hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象 哈希类型hash——基本命令 hset /hget /hms ...

  9. nmap使用帮助翻译

    Nmap 7.60 ( https://nmap.org )Usage: nmap [扫描类型] [操作] {目标说明}目标说明:  可以识别主机名.IP地址.网络,等等.  例如: scanme.n ...

  10. AJAX一些注释掉的语句

    var sysdept=JSON.parse(localStorage.getItem("loginSysUser")); for(var o in sysdept){ alert ...