When working with
staticor global variables in C or C++, we know the compiler allocates storage for them in the application's data segment. However, when the variable is an instance of a C++ class, it must be initialized as well as allocated. While the C++ language specification explains when this initialization takes place, it is a rather simple matter to demonstrate the rules for clarity.
After working with C++ for a while, we all come across situations where we need to create one and only one instance of a given object type. However, depending on your needs, there's more than one way to go about implementing a singleton.
In addition to the
inlinemethod modifiers, C++ also allows the addition of the
constmodifier on class methods. The
constmodifier indicates the method does not alter the state of class member variables.
C++ offers two useful modifiers for class methods:
virtualmethod can inherit new behavior from derived classes, while the
inlinemodifier requests that the compiler place the content of the method "inline" wherever the method is invoked rather than having a single instance of the code that is called from multiple places. You can think of it as simply having the compiler expand the content of your method wherever you invoke the method. But how does the compiler handle these modifiers when they are used together?
One of the first things we learn about multithreaded programming is the need to guard against simultaneous read-write access to shared resources, but this isn't always a simple matter.