Initialization, assignment, and sectations of C ++ Primer

xiaoxiao2021-03-06  19

14.1 initialization

Samples like this:

Class Data

{

PUBLIC:

int IVAL

Char * PTR;

}

Can be initialized as follows without providing constructor:

Data local1 = {0, 0}; // is called an explicit initialization table

This is because its data is public.

For most classes, it is a good choice for the initialization data member for most classes. However, the above initialization form is particularly useful in initializing large data structures, such as palette classes, saving constructive call overhead.

14.2 constructor of class

The constructor is the same name, and cannot specify the return type.

The constructor can have multiple, but the parameter table must be unique.

When using New, the compiler will call the class constructor only when the memory is successfully obtained.

Only when there is no constructing function or default, we can define the type object without specifying a real-console collection. In practice, if other constructor is defined, it is recommended to provide a default constructor.

The constructor cannot be Const or Volatile.

By default, a single parameter constructor is used as a conversion operator. Use Keyword Explicit to suppress unintentional hermit transformations.

example:

Extern Void Print (Account & ACCT);

...

Int main ()

{

Print ("oos"); // Account constructor is implicitly called

}

14.2.1

Default constructor

The default constructor refers to the constructor that can be called without the user to specify the argument. This does not mean that it cannot accept the arguments, such as: iStack :: IStack (int isize = 0) is a default constructor.

If the class data member is public, and there is no user-defined constructor, the member initialization value of the class object depends on the context environment. If a static object, the initial value of the member initial value is 0. If the initial definition or dynamically allocated object, the initial value is random.

If there is no default constructor, the compiler does not necessarily generate one.

(In the Book, in the following four cases, the compiler will provide a class to provide default constructor:

1. The base class has a default constructor;

2. The member of its object has a default constructor;

3. It has a virtual function;

4. It has a virtual base class)

14.2.2

Restriction object creation

The constructor is non-public. The advantage of this is:

Prevent copies of a class object to the other object of the class;

It is pointed out that the constructor can be called when the class is only called by the base class.

14.2.3

Copy constructor

Assigning the different objects of the same class assigns each other, the default mode is initialized by member. When there is a pointer member, it is obvious that the copy constructor is required at this time.

The copy constructor has a reference to the class object as a parameter.

14.3 classifier function

The destructor is a special function provided by the user, and when the object of this class leaves its scope, or the delete operation is applied to the object pointer of the class, it will be automatically called.

When a class's data member is stored, the destructor is not required, (eg Float X, Y, Z).

The C language is guaranteed, and does not delete a pointer that does not point to any object.

14.3.1

Destructor

14.4 class object array

Types such as this: Account Table [3] = {"a", // Call a single parameter constructor

Accent ("B", 10.00) // Call multi-parameter constructor

// The third element does not specify, then call the default constructor (if present)

}

14.5 Members Initialization Table does not exist implicit conversions from the String class to Char *, but String class supports the switch from char * to String.

Through member initialization table, class data members can be explicitly initialized.

The implementation of the constructor is divided into two phases: implicit or explicit initialization phase; the calculation execution phase, consisting of all statements in the constructor.

Initialization is an implicit, base class, and all member class objects that will be called.

In addition to ConST and reference data members (these two only members initialization tables), the results and performance of the initialization list and the use of assignment statements are equivalent.

The initialization order corresponds to the order of the class member object being declared, and is independent of the order of the initialization.

The initialization of members in the initialization list is always a member of the assignment in the constructor. To avoid errors, it is generally recommended to initialize the assignment of another member in the constructor.

14.6 initialization by member

Initialize another class object with a class object, such as:

1. Explicitly initialize another class object with a class object: Account NewAccount (Oldaccount);

2. Pass a class object to a function: Extern A (Account Account); if (A (Oldaccount)

3. Pass a class object as a return value of a function back.

4. Definition of non-empty sequential container

The default is to initialize the member, regardless of whether there is an explicit constructor.

It is easy to happen "alias" problem. You can solve it by providing a display copy constructor. In addition, it is also possible to initialize according to members through the following alternatives:

1. Declare a copy constructor of a private, which prevents anywhere in addition to member functions and friend functions by member initialization;

2. Do not provide the definition of this function. Compiled by but generate connection errors.

14.6.1

Initialization of member objects

If a class provides an explicit copy constructor, call the function to initialize, otherwise the default is initialized by member.

14.7 Assignment by Member

Similar to the initialization of members.

14.8 efficiency problem

NLV optimization, see depth exploration C object model.

So, try to use the following statement:

Matrix C = A B;

Instead:

Matrix C;

C = a b;

转载请注明原文地址:https://www.9cbs.com/read-67612.html

New Post(0)