1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
bf(C++) classes usually contain two special categories of member functions
which are essential to the proper working of classes. These categories are the
constructors and the i(destructor). The em(destructor)'s primary task is to
return memory allocated by an object to the common pool when an object goes
`out of scope'. Allocation of memory is discussed in chapter ref(MEMORY), and
an in-depth coverage of destructors is therefore postponed until we reach that
chapter. In the current chapter the emphasis is on the class's internal
organization and on its constructors.
Constructors are recognized by their names which are equal to their class
names. Constructors do not specify return values, not even tt(void). E.g.,
the class tt(Person) may define a constructor tt(Person::Person()). The
bf(C++) run-time system ensures that the constructor of a class is called when
a variable of the class is defined. It is possible to define a class lacking
any constructor. In that case the compiler defines a
hi(constructor: default) default constructor that is called when an object of
that class is defined. What actually happens in that case depends on the data
members that are defined by that class (cf. section ref(MemberInitializers)).
Objects may be defined locally or globally. However, in bf(C++) most objects
are defined locally. Globally defined objects are hardly ever required and are
somewhat deprecated.
When a function defines a local object, that object's constructor is called
every time the function is called. The object's constructor is activated at
the point where the object is defined (a subtlety is that an object may be
defined implicitly as, e.g., a temporary variable in an expression).
When an object is defined as a static object
hi(static object)hi(object: static/global) it is constructed when
the program starts. In this case its constructor is called even
before the function tt(main) starts. Example:
verbinclude(-a examples/emptymain.cc)
The program contains one global object of the class tt(Demo) with tt(main)
having an empty body. Nonetheless, the program produces some output generated
by the constructor of the globally defined tt(Demo) object.
Constructors have a very important and well-defined role. They must ensure
that all the class's data members have sensible or at least well-defined
values once the object has been constructed. We'll get back to this important
task shortly. The em(default constructor)hi(constructor: default) has no
argument. It is defined by the compiler unless another constructor is defined
and unless its definition is suppressed (cf. section ref(DEFAULTED)). If a
default constructor is required in addition to another constructor then the
default constructor must explicitly be defined as well. bf(C++) provides
special syntax to realize that without much effort, which is also covered by
section ref(DEFAULTED).
|