• C++类静态成员变量和const常量的初始化方法


    C++类静态成员变量和const常量在定义类的时候就必须初始化,否则都会编译出错。

    而具初始化方法为:


    C++类静态成员变量初始化方法

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cstring>
          
    using namespace std;
    class A{
    public:
         static void fun()
         {
              ab = 2;
              cout << ab << endl;
          }
    private:
         static int ab;
    };
    
    int A::ab = 10//在此初始化
    
    int main(int argc, char *argv[])
    {
         A::fun();
         return 0;
    }


    C++类const常量初始化方法:

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cstring>
          
    using namespace std;
    class A{
    public:
         A:ab(10)//在此初始化ab
         { ;}
         static void fun()
         {
              ab = 2;
              cout << ab << endl;
          }
    const int ab;
    
    };




  • 相关阅读:
    二分图最大匹配
    Problems about trees
    Hackerrank Going to the Office
    多校题解
    HDU #2966 In case of failure
    K-D Tree
    UOJ #10 pyx的难题
    bzoj 1090 字符串折叠
    uva 1347 旅行
    bzoj 1059 矩阵游戏
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3167782.html
Copyright © 2020-2023  润新知