How access class variables in c++

前端 未结 4 1836
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 00:44

Is it possible in c++ to access class variables in other classes without creating an object. I have tried to use static, but the other class doesnt recognize my variable. I

4条回答
  •  独厮守ぢ
    2021-01-20 01:18

    class Myclass
    {
    
        public:
             static int i;
    };
    
    int Myclass::i = 10;
    
    
    class YourClass
    {
    
        public:
            void doSomething()
            {
                 Myclass::i = 10;  //This is how you access static member variables
            }
    
    };
    
    int main()
    {
        YourClass obj;
        obj.doSomething();
        return 0;
    }
    

提交回复
热议问题