Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static variable in method call

If you create a local static variable inside a method, is that initialised once per instance, or once per program?

Does this differ between C++ and Objective-C?

like image 438
Dollarslice Avatar asked Jan 20 '12 15:01

Dollarslice


1 Answers

If you create a local static variable inside a method, is that initialised once per instance, or once per program?

Once per program.

Even if it is in a non-static class member function, it is not associated with any class instance; there will only be one instance of the variable in the whole program, initialised just once.

Does this differ between C++ and Objective-C?

In C++, it is initialised the first time the function is called. In C (and Objective-C), it is initialised prior to program startup. In practice, this doesn't make a difference, since the initialisation can't have any side effects in C.

like image 91
Mike Seymour Avatar answered Sep 24 '22 22:09

Mike Seymour