Classes (C++)

Default Constructor
A default constructor is a constructor that can be called without supplying an argument. If a user has declared a default constructor, that one will be used; otherwise, the compiler will try to generate one if needed and if the user hasn't declared other constructors. A compiler-generated default constructor implicitly calls the default constructors for a class' members of class type and bases.

Because consts and references must be initialized, a class containing const or reference members cannot be default-constructed unless the programmer explicitly supplies a constructor. For example:

Copy Constructor
By default, class objects can be copied. In particular, a class object can be initialized with a copy of another object of the same class. This can be done even where constructors have been declared. For example:

By default, the copy of a class object is a copy of each member. If that default is not the behavior wanted for a class X, a more appropriate behavior can be provided by defining a copy constructor,.

A memberwise copy can cause unwanted effects when used on objects of a class with pointer members. Memberwise copy is usually the wrong semantics for copying objects containing resources managed by a constructor/destructor pair. For example:

Here, the Table default constructor is called twice: once each for t1 and t3. It is not called for t2 because that variable was initialized by copying. However, the Table destructor is called three times. The default interpretation of assignemnt is memberwise copy, so t1, t2, and t3 will, at the end of h, each contain a pointer to the array of names allocated on the free store when t1 was created. No pointer to the array of names allocated when t3 was created remains because it was overwritten by the t3 = t2 assignment. Thus its storage will be lost to the program forever. On the other hand, the array created for t1 appears in t1, t2, and t3, so it will be deleted thrice. The result of that is undefined and probably disastrous.

Such anomalies can be avoided by defining what it means to copy a Table:

The programmer can define any suitable meaning for these copy operations, but the traditional one for this kind of container is to copy the contained elements (or at least to give the user of the container the appearance that a copy has been done). For example:

As is almost always the case, the copy constructor and the copy assignment differ considerably. The fundamental reason is that a copy constructor initializes uninitialized memory, whereas the copy assignment operator must correctly deal with a well-constructed object.

Assignment can be optimized in some cases, but the general strategy for an assignment operator is simple: protect against self-assignment, delete old elements, initialize, and copy in new elements. Usually every nonstatic member must be copied.

Destructors
Destructors typically clean up and release resources after use. They are called implicitly when an automatic variable goes out of scope, an object of the free store is deleted, etc. Only in very unusual circumstances does the user need to call a destructor explicitly. The most common use of a destructor is to release memory acquired in a constructor.

A matching constructor/destructor pair is the usual mechanism for implementing the notion of a variably-sized object in C++.

Initialization

 * Class string initialize to empty string ("")
 * Arrays initialize to the address of the first datacell in the array

Initialization Lists
Arguments for a member's constructor can be specified in a member initializer list of the constructor. The member initializers are preceded by a colon and the individual member initializers are separated by commas.

The member's constructors ar ecalled before the body of the containing class' own constructor is executed. The constructors are called in the order in which they are declared in the class rather than the order in which they appear in the initializer list. To avoid confusion, it is best to specify the initializers in the declaration order. The member destructors are called in the reverse order of construction.

Static Members
If a class is dependent upon a global variable, the class can be used only in a context where the global variable is defined and correctly used by every piece of code. This is the kind of constraint that causes a class to be useless outside the context in which it was first written. This style leads to code that is useless except to its original programmer. It should be avoided.

Fortunantely, we can get the convenience without the encumbrance of a publicly accessible global variable. A variable that is part of a class, yet is not part of an object of that class, is called a static member. There is exactly one copy of a static member instead of one copy per object, as for ordinary non-static members. Similarly, a function that needs access to members of a class, yet 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.