1. 程式人生 > 其它 >【C++語法】關鍵字

【C++語法】關鍵字

https://tlanyan.pp.ua/cpp-function-modifier-summary/

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14
typedef int myint
int main(){
    const int sz = get_size();    //const objects: must be initialized  when created. Not a const expression
    constexpr int num = 315;    //constant expression: value canbe evaluated at complie time
    int arr[num];    //array dimension (as part of the array's type) must be know at compile time
    /* array 
    ** int a2[] = a1;    //error: cannot initialize one array with another
    ** int *ptr[10];    //array of ten pointers to int
    ** int (*Ptr)[10] = &arr    //point to an array of ten ints
    */
    return 0;
}

Static與Const的區別
C++ static、const 和 static const 型別成員變數宣告以及初始化

const

  • const 成員變數也不能在類定義處初始化,只能通過建構函式初始化列表進行,並且必須有建構函式

static

  • outside the class, do not repeat the static keyword;
  • static member functions do not have this pointer, may not be declared as const;
  • static data members are not initialized by the constructors, they are usually defined and initialized outside the class body. (const integral type can be initialized inside the class body)
  • static data members can be the class, whereas nonstatic data members must be declared as pointers or references to the class;
  • static data members can be used as default argument
from Chu