John
John

Reputation: 6648

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.

Upvotes: 5

Views: 3080

Answers (1)

Griwes
Griwes

Reputation: 9031

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;

Upvotes: 3

Related Questions