简体   繁体   中英

How static variable in classes being allocated in memory?

class car
{
public:
    static int i;
};

int car::i = 7;
int main ()
{   
    cout << car::i;
    return 0;    
}

Is a static variable allocating memory because of it's definition outside class and declaring it inside class just to become a member of class? and why when it becomes constant we didn't define it outside class?

I understand that static variable being allocated in global and static variable segment in memory and static variable don't belong to any object.

How static variable in classes being allocated in memory?

Static class variables have static storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM