关于类的静态成员的知识点


class ct{
public:
    int x = 9;
    static int y ;
//    static int a = 67; //error: ISO C++ forbids in-class initialization of non-const static member ‘ct::a’
    static constexpr int b = 13;
    static ct ct_copy;
    ct* ct_copy_2;
//    ct ct_copy_3;//error: field ‘ct_copy_3’ has incomplete type ‘ct’
private:
    static constexpr int z = 30;//例外,在类内初始化必须要加constexpr
    int t = 15;
    int init(int ds = y){return 6;}
//    int sni(int mi = x){return 32;} //error: invalid use of non-static data member ‘ct::x’
};
//static 对象必须在类外部定义和初始化 
//int ct::y = t; //error: invalid use of non-static data member ‘ct::t’

// 可以这样使用
//ct ct1;
//int ct::y = ct1.t;


//int ct::y = 14;  //正确
int ct::y = z;     //正确,可以访问私有成员



int
main() {


    return 0;
}

 

你可能感兴趣的:(C++)