• 面向对象程序设计-C++_课时28静态对象_课时29静态成员


    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)

    可以编译,链接失败

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

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A() { i = 0; }
     8     void print() { std::cout << i << std::endl; }
     9     void set(int ii) { i = ii; }
    10 private:
    11     static int i;
    12 };
    13 
    14 void main()
    15 {
    16     A a, b;
    17 
    18     //error LNK2001: 无法解析的外部符号 "private: static int A::i" (?i@A@@0HA)
    19 
    20     a.set(10);
    21     b.print();
    22     
    23     system("pause");
    24 }

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

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

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A() :i(10) { }//error C2438: “i”: 无法通过构造函数初始化静态类数据
     8     void print() { std::cout << i << std::endl; }
     9     void set(int ii) { i = ii; }
    10 private:
    11     static int i;
    12 };
    13 
    14 int A::i;
    15 
    16 void main()
    17 {
    18     A a, b;
    19 
    20     a.set(10);
    21     b.print();
    22     
    23     system("pause");
    24 }

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

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A() { }
     8     void print() { std::cout << i << std::endl; }
     9     void set(int i) { this->i = i; }//静态数据成员有this指针,静态成员函数没有this指针
    10 private:
    11     static int i;
    12 };
    13 
    14 int A::i = 20;
    15 
    16 void main()
    17 {
    18     A a, b;
    19 
    20     a.set(10);
    21     b.print();
    22     
    23     system("pause");
    24 }

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

    通过对象都可以访问i

    通过类都可以访问i

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A() { }
     8     void print() { std::cout << i << std::endl; }
     9     void set(int i) { this->i = i; }
    10     static int i;
    11 };
    12 
    13 int A::i = 20;
    14 
    15 void main()
    16 {
    17     A a, b;
    18 
    19     a.set(10);
    20     b.print();
    21 
    22     std::cout << a.i << std::endl;//通过对象都可以访问i
    23     std::cout << A::i << std::endl;//通过类都可以访问i
    24     
    25     system("pause");
    26 }

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

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A() { }
     8     void print() { std::cout << i << std::endl; }
     9     void set(int i) { this->i = i; }
    10 private:
    11     static int i;
    12 };
    13 
    14 int A::i = 20;
    15 
    16 void main()
    17 {
    18     A a, b;
    19 
    20     a.set(10);
    21     b.print();
    22 
    23     std::cout << a.i << std::endl;//error C2248: “A::i”: 无法访问 private 成员(在“A”类中声明)
    24     std::cout << A::i << std::endl;//error C2248: “A::i”: 无法访问 private 成员(在“A”类中声明)
    25     
    26     system("pause");
    27 }

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

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A() { }
     8     void print() { std::cout << i << std::endl; }
     9     void set(int i) { this->i = i; }
    10     static void say(int ii) { std::cout << ii << " " << k << endl; }//error C2597: 对非静态成员“A::k”的非法引用
    11 private:
    12     int k;
    13     static int i;
    14 };
    15 
    16 int A::i = 20;
    17 
    18 void main()
    19 {
    20     A a, b;
    21 
    22     a.set(10);
    23     b.print();
    24 
    25     a.say(0);
    26     A::say(0);
    27     
    28     system("pause");
    29 }

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

    静态成员函数没有this指针

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

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

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A() { }
     8     void print() { std::cout << i << std::endl; }
     9     void set(int i) { this->i = i; }
    10     static void say(int ii) { std::cout << ii << " " << this->i << endl; }//静态成员函数没有this指针
    11 
    12     //1>main.cpp(10) : error C2355 : “this” : 只能在非静态成员函数或非静态数据成员初始值设定项的内部引用
    13     //    1>main.cpp(10) : error C2227 : “->i”的左边必须指向类 / 结构 / 联合 / 泛型类型
    14 
    15 private:
    16     int k;
    17     static int i;
    18 };
    19 
    20 int A::i = 20;
    21 
    22 void main()
    23 {
    24     A a, b;
    25 
    26     a.set(10);
    27     b.print();
    28 
    29     a.say(0);
    30     A::say(0);
    31     
    32     system("pause");
    33 }
  • 相关阅读:
    换个格式输出整数
    c++入门构造函数
    接口继承与实现
    spring mvc 之@requestmapping
    RabbitMQ
    Socket通信原理
    TCP三次握手与四次挥手
    WIN 10家庭版没有Hyper-V
    aop 切面demo
    JVM之旅------jvm内存模型
  • 原文地址:https://www.cnblogs.com/denggelin/p/5637939.html
Copyright © 2020-2023  润新知