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. 【自学php】第一天-macbook上配置php

    今天MacBook到手了,就正式开始学习php了.先搭个环境,由于MacBook自带了Apache和php所以只要修改下配置启动就可以了. 1.启用root用户(如果不启用root,下面的命令前都要加 ...

  2. MacBook USB Type-C接口很美?其实是缩水的!

    苹果终于推出了12寸的全新MacBook,拥有2304×1440的高分辨率.蝶式结构全尺寸键盘.新的触摸板.14nm Core M处理器和无风扇设计,以及新的USB 3.1 Type-C接口.可以预料 ...

  3. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  4. Recover a file when you use git reset head by mistake.

    $ git init Initialized empty Git repository in .git/ $ echo "testing reset" > file1 $ g ...

  5. Struts2 使用通配符动态请求Action

    在以前的学习中,<action>元素的配置,都是用明确的配置,其name.class等属性都是一个明确的值.其实Struts2还支持class属性和method属性使用来自name属性的通 ...

  6. 全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型

    函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...

  7. 自定义UICollectionViewLayout 实现瀑布流

    今天研究了一下自定义UICollectionViewLayout. 看了看官方文档,要自定义UICollectionViewLayout,需要创建一个UICollectionViewLayout的子类 ...

  8. 关于EJB 时间注解与oracle数据库时间格式

    EJB中Temporal是时间注解,其中TemporalType是时间注解的枚举类型其中包括 TemporalType类型,请看源码/*** Type used to indicate a speci ...

  9. Chrome 常用快捷键

    20160518     生活常识     Chrome常用操作快捷键 掌握Chrome的常用快捷键,不仅可以节约时间,还能够提高工作效率,最主要还可以装逼.以下是一些常用快捷键: 窗口操作快捷键: ...

  10. mysql 按日期查询

    在mysql中,比如你的表的时间字段是column2,并且column2的类型是timestamp 单日查询: select * from TableName where column1='xxxx' ...