Skip to Main Content
C++ In a Nutshell
book

C++ In a Nutshell

by Ray Lischner
May 2003
Intermediate to advanced content levelIntermediate to advanced
808 pages
32h 24m
English
O'Reilly Media, Inc.
Content preview from C++ In a Nutshell

Name

static storage class — Static storage class specifier

Synopsis

               storage-class-specifier := static

The static storage class specifier can be used with objects, functions, and class members. Its purpose varies slightly depending on how you use it:

  • For local objects, static affects the object’s lifetime: instead of being constructed anew every time the function is called, a static local object is constructed at most once, and the object retains its value across function calls.

  • A static class member is not tied to a specific object. Instead, it is similar to a global object or function, but in the scope of the class that declares it.

  • For global declarations, using static means the object or function has internal linkage instead of the default external linkage, that is, the object or function is not visible in other source files. This use is deprecated; use anonymous namespaces instead.

Example

struct demo {static const int size = 10;
  static void func(  ) {
    return size;
  }
private:
  int data_[size];
};
static int local_data = 10;
static int local_func() { return demo::func(  ); }

See Also

auto, class, extern, namespace, register, type, Chapter 2

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ High Performance

C++ High Performance

Viktor Sehr, Björn Andrist
Optimized C++

Optimized C++

Kurt Guntheroth
Mastering C++ Programming

Mastering C++ Programming

Jeganathan Swaminathan

Publisher Resources

ISBN: 059600298XSupplemental ContentErrata Page