zoukankan      html  css  js  c++  java
  • (学习笔记)静态常量成员变量的初始化

      *******************************************************************
      It is now possible to initialize integral constant static members inside the class structure.
      This is useful when the constants is used in the class structure after the initialization.
      (对于当这个常量会在初始化后要使用的情况,这样做会比较有效)
      For example ===>
       class MyClass{
       static const int NUM = 100;
       int elements[NUM];
       ...
       };
      Note that you still have to to define space for a constant static member that is initialized within a class definition:
       const int MyClass::NUM; //no initialization here
      */
      #pragma warning(disable:4530)
      #include <iostream>
      using namespace std;
      class A
      {
      public:
      A(){
       for(int i=0; i<SIZE; i++)
       num[i] = i;
      }
      void print(){
       for(int i=0; i<SIZE; i++)
       cout<<num[i]<<" "<<endl;
      }
      private:
      static const SIZE = 10;
      //注意:由于SIZE作为一个const常量,所以必须在首次定义的时候就给它赋值。
      int num[SIZE];
      };
      const int A::SIZE;
      //注意:由于SIZE作为一个类的静态成员,应该在类体外部定义(以取得和全局变量类似的效用)
      int main()
      {
      A a;
      a.print();
      return 0;
      }
    大部分转载 小部分自写
  • 相关阅读:
    Poj1163 The Triangle(动态规划求最大权值的路径)
    Poj1258_Agri-Net(最小生成树)
    Poj1258_Agri-Net(最小生成树)
    Poj1218_THE DRUNK JAILER(水题)
    Poj1218_THE DRUNK JAILER(水题)
    Poj1298_The Hardest Problem Ever(水题)
    Poj1298_The Hardest Problem Ever(水题)
    Poj1012_Joseph
    Poj1012_Joseph
    Poj_1008--Maya Calendar
  • 原文地址:https://www.cnblogs.com/8586/p/1255773.html
Copyright © 2011-2022 走看看