course of C++ programming language
lecture 6: free store
Static members
A variable that is part of a class, yet is not part of an object of that class, is called a static data member. There is exactly one copy of a static member instead of one copy per object, as for static ordinary non-static members. Similarly, a function that needs access to members of a class, yet static doesn't need to be invoked for a particular object, is called a static member function.
A static member can be referred to like any other member. In addition, a static member can be referred to without mentioning an object. Instead, its name is qualified by the name of its class.
There is a single piece of storage for a static data member, regardless of how many objects of that class you create. All objects share the same static storage space for that data member, so it is a way for them to "communicate" with each other. But the static data belongs to the class; its name is scoped inside the class and it can be public, private, or protected.
Operators new and delete
You can create and destroy objects at runtime. C++ has always provided the dynamic memory allocation operators new and delete that allocate storage from the heap (also called the free store) at runtime.
An object created on the free store has its constructor invoked by the new operator and exists until the delete operator is applied to a pointer to it.
When you create an object with new (using a new-expression), it allocates enough storage on the heap to hold the object and calls the constructor for that storage. You can create a new-expression using any constructor available for the class. If the constructor has no arguments, you write the new-expression without the constructor argument list.
The complement to the new-expression is the delete-expression, which first calls the destructor and then releases the memory. Just as a new-expression returns a pointer to the object, a delete-expression requires the address of an object.
If the pointer you're deleting is zero, nothing will happen. For this reason, people often recommend setting a pointer to zero immediately after you delete it, to prevent deleting it twice. Deleting an object more than once is definitely a bad thing to do, and will cause problems.
new and delete for arrays
In C++, you can create arrays of objects on the stack or on the heap. The constructor will be called for each object in the array. There's one constraint, however: there must be a default constructor, because a constructor with no arguments must be called for every object. There is no way to specify explicit arguments for a constructor in an array declaration.
You can create arrays of objects on the heap using new[] operator.
There is a special destruction operator for arrays, delete[].
The empty brackets tell the compiler to generate code that fetches the number of objects in the array, stored somewhere when the array is created, and calls the destructor for that many array objects.
Something about exceptions
...
References
-
B.Stroustrup: The C++ programming language. Third edition.
Section 10: classes; pp. 223-258. -
Bruce Eckel: Thinking in C++. Second edition.
Section 4: data abstarction; pp. 217-254.
Section 5: hiding the implementation; pp. 259-280.
Section 6: initialization and cleanup; pp. 283-306.
Section 8: constants; pp. 352-367.
Section 11: references and copy-constructor; pp. 455-479.