简体   繁体   中英

Initialize non-const static member variables in C++, through a static member function

I am trying the following and getting an emulator crash between the two log statements. Is there something wrong?

protected:
    static int maxSize;
public:
    static void setFontSizeRange(int max) {
        Log("here %d->%d", max, maxSize);
        maxSize = max;
        Log("ok");
    }

I can get the log to reproduce the parameter but it crashes before outputting the static member (so the first log shown above would not work while it refers to that).

Thanks.

You should define the static member.

class Something
{
protected:
    static int maxSize;
public:
    static void setFontSizeRange(int max) {
        Log("here %d->%d", max, maxSize);
        maxSize = max;
        Log("ok");
    }
}; // class declaration ends here...

int Something::maxSize = 0;

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