Static in C++

Two basic meanings

Static Storage

--allocated once at a fixed address

Visibility of a name

--internal linkage

Don't use static except inside functions and classes.

Uses of "static" in C++

Static free functions----deprecated弃用

Static globle variables----deprecated弃用

Static local variables----Persistent storage持久存储

Static member variables----Shared by all instances所有对象共享

Static member functions----Shared by all instances, can only access static member variables所有对象共享,只能访问静态变量或静态函数

Static inside functions

Value is remembered for entire program

Initialization occurs only once

Static applied to objects...

Construction occurs when definition is encountered

--Constructor called at-most once

--The constructor arguments must be satisfied

Destruction takes place on exit form program

--Compiler assures LIFO order of destructors

Can we apply static to members?

Static means

--Hidden

--Persistant

Hidden: A static member is a member

--Obeys usual access rules

Persistant: Independent of instances

error LNK2001: 无法解析的外部符号 "private: static int A::i" (?i@A@@0HA)

可以编译,链接失败

写在类里面的都是声明,不是定义

 #include <iostream>
using namespace std; class A
{
public:
A() { i = ; }
void print() { std::cout << i << std::endl; }
void set(int ii) { i = ii; }
private:
static int i;
}; void main()
{
A a, b; //error LNK2001: 无法解析的外部符号 "private: static int A::i" (?i@A@@0HA) a.set();
b.print(); system("pause");
}

error C2438: “i”: 无法通过构造函数初始化静态类数据

初始化列表,无法初始化静态类数据

 #include <iostream>
using namespace std; class A
{
public:
A() :i() { }//error C2438: “i”: 无法通过构造函数初始化静态类数据
void print() { std::cout << i << std::endl; }
void set(int ii) { i = ii; }
private:
static int i;
}; int A::i; void main()
{
A a, b; a.set();
b.print(); system("pause");
}

静态数据成员有this指针,静态成员函数没有this指针

 #include <iostream>
using namespace std; class A
{
public:
A() { }
void print() { std::cout << i << std::endl; }
void set(int i) { this->i = i; }//静态数据成员有this指针,静态成员函数没有this指针
private:
static int i;
}; int A::i = ; void main()
{
A a, b; a.set();
b.print(); system("pause");
}

static静态数据成员实际上是全局变量

通过对象都可以访问i

通过类都可以访问i

 #include <iostream>
using namespace std; class A
{
public:
A() { }
void print() { std::cout << i << std::endl; }
void set(int i) { this->i = i; }
static int i;
}; int A::i = ; void main()
{
A a, b; a.set();
b.print(); std::cout << a.i << std::endl;//通过对象都可以访问i
std::cout << A::i << std::endl;//通过类都可以访问i system("pause");
}

error C2248: “A::i”: 无法访问 private 成员(在“A”类中声明)

 #include <iostream>
using namespace std; class A
{
public:
A() { }
void print() { std::cout << i << std::endl; }
void set(int i) { this->i = i; }
private:
static int i;
}; int A::i = ; void main()
{
A a, b; a.set();
b.print(); std::cout << a.i << std::endl;//error C2248: “A::i”: 无法访问 private 成员(在“A”类中声明)
std::cout << A::i << std::endl;//error C2248: “A::i”: 无法访问 private 成员(在“A”类中声明) system("pause");
}

error C2597: 对非静态成员“A::k”的非法引用

 #include <iostream>
using namespace std; class A
{
public:
A() { }
void print() { std::cout << i << std::endl; }
void set(int i) { this->i = i; }
static void say(int ii) { std::cout << ii << " " << k << endl; }//error C2597: 对非静态成员“A::k”的非法引用
private:
int k;
static int i;
}; int A::i = ; void main()
{
A a, b; a.set();
b.print(); a.say();
A::say(); system("pause");
}

虽然还没有建立类的对象,但可以访问静态成员。为了实现,因此没有this指针。

静态成员函数没有this指针

error C2355: “this”: 只能在非静态成员函数或非静态数据成员初始值设定项的内部引用

error C2227: “->i”的左边必须指向类/结构/联合/泛型类型

 #include <iostream>
using namespace std; class A
{
public:
A() { }
void print() { std::cout << i << std::endl; }
void set(int i) { this->i = i; }
static void say(int ii) { std::cout << ii << " " << this->i << endl; }//静态成员函数没有this指针 //1>main.cpp(10) : error C2355 : “this” : 只能在非静态成员函数或非静态数据成员初始值设定项的内部引用
// 1>main.cpp(10) : error C2227 : “->i”的左边必须指向类 / 结构 / 联合 / 泛型类型 private:
int k;
static int i;
}; int A::i = ; void main()
{
A a, b; a.set();
b.print(); a.say();
A::say(); system("pause");
}

面向对象程序设计-C++_课时28静态对象_课时29静态成员的更多相关文章

  1. Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?

    什么是面向对象程序设计? 我们称为OOP(Object  Oriented  Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...

  2. Struts2_day03--课程安排_OGNL概述入门_什么是值栈_获取值栈对象_值栈内部结构

    Struts2_day03 上节内容 今天内容 OGNL概述 OGNL入门案例 什么是值栈 获取值栈对象 值栈内部结构 向值栈放数据 向值栈放对象 向值栈放list集合 从值栈获取数据 获取字符串 获 ...

  3. 『PyTorch』第五弹_深入理解Tensor对象_中下:数学计算以及numpy比较_&_广播原理简介

    一.简单数学操作 1.逐元素操作 t.clamp(a,min=2,max=4)近似于tf.clip_by_value(A, min, max),修剪值域. a = t.arange(0,6).view ...

  4. 『PyTorch』第五弹_深入理解Tensor对象_下:从内存看Tensor

    Tensor存储结构如下, 如图所示,实际上很可能多个信息区对应于同一个存储区,也就是上一节我们说到的,初始化或者普通索引时经常会有这种情况. 一.几种共享内存的情况 view a = t.arang ...

  5. 『PyTorch』第五弹_深入理解Tensor对象_中上:索引

    一.普通索引 示例 a = t.Tensor(4,5) print(a) print(a[0:1,:2]) print(a[0,:2]) # 注意和前一种索引出来的值相同,shape不同 print( ...

  6. 『PyTorch』第五弹_深入理解Tensor对象_上:初始化以及尺寸调整

    一.创建Tensor 特殊方法: t.arange(1,6,2)t.linspace(1,10,3)t.randn(2,3) # 标准分布,*size t.randperm(5) # 随机排序,从0到 ...

  7. 201871010101-陈来弟《面向对象程序设计(java)》第四周学习总结

                                                                                                        ...

  8. C++面向对象程序设计之类和对象的特性

    类和对象的属性 注意:本文为书籍摘要版,适合有一定程序基础的人阅读. 2.1 面向对象程序设计方法概述 2.1.1 什么是面向对象的程序设计 1.对象 客观世界中的任何一个事物都可以看成一个对象. 如 ...

  9. Python基础(16)_面向对象程序设计(类、继承、派生、组合、接口)

    一.面向过程程序设计与面向对象程序设计 面向过程的程序设计:核心是过程,过程就解决问题的步骤,基于该思想设计程序就像是在设计一条流水线,是一种机械式的思维方式 优点:复杂的问题的简单化,流程化 缺点: ...

随机推荐

  1. mysql性能优化学习笔记(4)索引的优化

    一.选择合适的索引列     1.在where,group by,order by,on从句中出现的列     2.索引字段越小越好(因为数据库的存储单位是页,一页中能存下的数据越多越好 )      ...

  2. wifi 攻破

    链接1 wifi 加密方式 1,wep加密 2.WPA/WPA2-PSK加密 WPA2 的破解方式: 1 爆力破解 2,pin 破解 1) 先破解 pin 码 2)再用 minidwep-gtk 破解

  3. eclipse IDE 扩展pydev

    1. 安装PyDev. 运行Eclipse,打开菜单Help->Install New Software.在work with里输入网址:http://pydev.org/updates ,然后 ...

  4. MYSQL 关闭二进制日志

    方法 set sql_log_bin = 0; ---------------------------------------------------------------------------- ...

  5. How to convert string to wstring?

    How to convert string to wstring? - Codejie's C++ Space - C++博客     How to convert string to wstring ...

  6. #include <windows.h>

      1 FindWindowA 2 keybd_event 3 malloc 4 MessageBox 5 MessageBoxA 6 MessageBoxW 7 mouse_event 8 SetC ...

  7. javascript时间处理方法收集

    首先收集到的是一个给某一个时间对象增加一段时间的方法, 例如2026-05-11增加一个月的时间,增加后时间为2026-05-11, 代码如下: function DateAdd(interval,n ...

  8. CodeForces463C Gargari and Bishops(贪心)

    CodeForces463C Gargari and Bishops(贪心) CodeForces463C 题目大意:  在国际象棋的棋盘上放两个主教,这个两个主教不能攻击到同一个格子,最后的得分是这 ...

  9. CSS ::before 和 ::after 伪元素用法

    CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前几天发现了 Creative Link Effects 这个非常有意思 ...

  10. Repeater获取某一行TextBox值

    TextBox tb = (TextBox)e.Item.FindControl("TextBoxID");