Chapter 12 Classes

来源:互联网 发布:台州椒江区网络诈骗 编辑:程序博客网 时间:2024/06/06 20:11

12.4 -- Constructors

If a class defines even one constructor, then the compiler will not generate the default constructor.


The synthesized default constructor initializes members using the same rules as those that apply for how variables are initialized. Members that are of class type are initialized by running each member's own default constructor. Members of built-in or compound type, such as pointers and arrays, are initialized only for objects that are defined at global scope. When objects are defined at local scope, then members of built-in or compound type are uninitialized.


Best Practices:

If a class contains data members of built-in or compound type, then the class should not rely on the synthesized default constructor. It should define its own constructor to initialize these members.


Best Practices:

In practice, it is almost always right to provide a default constructor if other constructors are being defined. Ordinarily the initial values given to the members in the default constructor should indicate that the object is "empty."


A constructor that can be called with a single argument defines animplicit conversionfrom the parameter type to the class type.

We can prevent the use of a constructor in a context that requries an implicit conversion by declaring the constructorexplicit.


12.6 -- static Class Members

Just as a class may define shared static data members, it may also define static member functions. A static member function has no this parameter. It may directly access the static members of its class but may not directly use the nonstatic members.


When we define a static member outside the class, we do not respecify the static keyword. The keyword appears only with the declaration inside the class body.


Integral const static Members Are Special

Ordinarily, class static members, like ordinary data members, cannot be initialized in the class body. Instead, static data members are normally initialized when they are defined.

One exception to this rule is that a const static data member of integral type can be initialized within the class body as long as the initializer is a constant expression:

class Account {     public:         static double rate() { return interestRate; }         static void rate(double);  // sets a new rate     private:         static const int period = 30; // interest posted every 30 days         double daily_tbl[period]; // ok: period is constant expression     };

A const static data member of integral type initialized with a constant value is a constant expression. As such, it can be used where a constant expression is required, such as to specify the dimension for the array member daily_tbl.

NOTE: When a const static data member is initialized in the class body, the data member must still be defined outside the class definition.

When an initializer is provided inside the class, the definition of the member must not specify an initial value:

// definition of static member with no initializer;// the initial value is specified inside the class definition   const int Account::period;