Initialization of Static Class members

后端 未结 1 1270
旧巷少年郎
旧巷少年郎 2020-12-18 16:35

If i try to initialize obj_s it asks me to make it const - and i cant do that for i have to keep count of my Created Objects.

#incl         


        
相关标签:
1条回答
  • 2020-12-18 16:58

    [Solved]

    The code is giving the error because the object is not getting created in the second case, and in the first its not initializing, the way its supposed to - Here's the fixed Code:

    #include<iostream>
    class A
    {
            static int obj_s;
    public: 
            A()
    {  obj_s++;  std::cout << A::obj_s << "\nObject(s) Created\n" ;  }
    }; 
    
    int A::obj_s=0;  // This is how you intialize it
    
    int main()
    {
    A a ,b,c,d;
    }
    
    0 讨论(0)
提交回复
热议问题