Skip to content

Instantly share code, notes, and snippets.

@sy6sy2
Last active June 9, 2020 15:24
Show Gist options
  • Select an option

  • Save sy6sy2/0e36a4522108d6ac1cb89e58ee1e42e4 to your computer and use it in GitHub Desktop.

Select an option

Save sy6sy2/0e36a4522108d6ac1cb89e58ee1e42e4 to your computer and use it in GitHub Desktop.
[C++ static keyword] #CPP

C++ static keyword

Static variable inside function

Static variables when used inside function are initialized only once, and then they hold there value even through function calls. These static variables are stored on static storage area , not in stack.

Static class objects

Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program. Static objects are also initialized using constructors like other normal objects. Assignment to zero, on using static keyword is only for primitive datatypes, not for user defined datatypes.

Static member variables in class

Static member variables of class are those members which are shared by all the objects. Static member variables are not initialied using constructor, because these are not dependent on object initialization. Also, it must be initialized explicitly, always outside the class. If not initialized, Linker will give error.

Static member functions

These functions work for the class as whole rather than for a particular object of a class. It can be called using an object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment