Mastering C++: Part 1: Static

Learning C++

Using C++

Mastering C++


Static

When thinking in the object-oriented paradigm, there is often a need to share values across different object instances. When creating new ones, there will always been a single value that all can access and use. The keyword static helps with this.

Any variable or function defined as static will be defined once for all objects.

However, while the use of static will define it (allocate memory for it, in most cases), it must be given a value outside of it.

Instances and Shared Values

Normally, each new object has its own memory and values. With using the keyword static, values can be shared across all of them.

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.