C ++ Primer learning summary chapter 2 class, Chapter 4 primer
Chapter 1 category
1. Introduce const member functions (C ++ Primer P231-232)
The constant object of the C ++ class cannot call non-const member functions. To call a member function of a constant object, it must be declared as const:
2. If the const member function returns * this after the end of a class, the returned type must beConst Class Name &The preceding const is indispensable. Otherwise, it cannot be compiled.
3. default constructor P235-236
The compiler will synthesize a default constructor for us only when it finds that the class does not contain any constructor, and this function performs default initialization for the class members (If a class member has an internal class initial value, the default initialization will not be performed.).
For built-in type members, random values are assigned for local object initialization by default, and 0 for global object initialization by default.
For Class Members, the default constructor of the class is called by default.
CompilerMaybe not.Synthesize a class without a constructor. if Class A does not have A default constructor, but class A contains class B's object member B, and Class B does not have A default constructor, in this case, the compiler will not be able to use the constructor to initialize member B in Class A objects. Therefore, Class A will not have the default constructor for merging:
If a class constructor only assigns an initial value to some members, the remaining members obtain a default value (If there is an internal class initial value, it will be initialized directly. Otherwise, the default class initialization will be executed.).
4. Differences between copy and assignment P239
Objects are copied in several situations: initialization variables are passed in the form of values or an object is returned.
When the value assignment operator is used, the object Value assignment operation occurs.
5. youyuan P241
Class A defines two friends, namely the print () function and Class B function. Therefore, within the print () function or within Class B, the private member of Class A can be called x.
Note: If you want to directly cout <B. a. x <endl; is still wrong, because x is private. Even if Class A is a friend of Class B, you cannot directly access the private member of Class. youyuan means that you can access private members of other classes within the class or function.
Youyuan further exploration:
Suppose there are Class A and Class B. Now I want to define A function print () of Class A as A friend element in Class B. How should I define the sequence of order?
The preceding sequence should be followed: Class B should be declared first, and Class A should be defined again. However, the print function of Class A can only be declared and cannot be defined.
It can be understood as follows:
Class A is defined before class B (but print () only declares not to be defined ):Since class B wants to use A member function print () of Class A as A friend, it is obvious that Class A must be defined before class B, so that you can reference the print () member function of Class A when defining Class B.
Class B declaration prior to Class A definition:Class B. Because the print () member function has the form parameters of class B, you need to declare class B before defining class.
Class B is defined prior to the print () function:Because Class B is used in the definition of the print () function, Class B must be fully defined when you define the print () function.
Youyuan function declaration:
Suppose Class A declares the friend function print () with friend (). print () can be declared first, but no code of any Class A can call print (). class A can call the print () function only after the print () function is declared globally. that is to say, when the print () function can be used depends only on whether it is declared normally.
6. variable data member (MutableKeyword) P245 can be changed even if it is a const object.
A is a constant object. a can only call the const member function.
7. Assume that class A has A set method and A print () method. Under what circumstances can we use the following:A. set (10). set (100). print ();When set () returns * this reference!
Because the vertex operator is left-bound, computation is performed on the left. At this time, the set returns the reference of * this, so you can continue to call other member functions.
Note: If the set () function is const, * this is a const object returned by it, then. set () cannot call the non-const print () function. for this, see the P247-248 page.
8.Incomplete type:A class is declared but not defined. P250
You can define a pointer or reference pointing to this type, or declare a function that takes an incomplete type as a parameter or return value. However, you cannot define an object of an incomplete type. Therefore:
A class can contain pointers or references pointing to its own type.
But it cannot contain objects of its own type.
9. type name scope: P255
The type name defined in the class can overwrite the type name defined outside the class,Premise:No non-class type names have been used before the current definition statement:
Because the money definition has been used when val is defined, it is wrong to define the new money type. If the new money type is placed in row 4th, it is correct.
10. const members, references, or class types that do not provide the default constructor must be initialized for some classes. If they are not initialized, they will be meaningless.
The Class A constructor above does not initialize v2 and v3, so it is wrong. the constructor of A should initialize the Initial Value List of v2 and v3. instead of assigning values directly in the constructor (Assignment is not initialized).
11. The initialization sequence of the members in the constructor is defined in the order they are defined in the class, rather than the order of the members in the Initial Value List:
V1 is initialized first, so v1 (v2) is initialized after v1 is a random value. v2, so v2 is 500.
12. Delegate constructor usage (P261, testedNon-C ++ 11 compilers can also be used)
NOTE: If constructor 1 Delegates constructor 2 to construct an object, then constructor 1 can call function 2 even before constructor 2,
13. The default constructor is automatically executed when the class object is initialized by default or when the value is initialized. Therefore, try to write a default constructor for each Class. Otherwise, errors may occur.
14.Conversion Constructor-> Implicit class type conversion (P295)
If the constructor only accepts one parameter, it actually defines an implicit conversion mechanism for this class type. That isThe parameter can be used to replace the class object.
In Class A, 100 and cin are used to replace Class A objects.
Only one class type conversion is allowed!However, the built-in type conversion supports multiple steps. That is, the preceding statement can be written as a1 = a1 + 100.3. First, convert 100.3 to int (Built-in Conversion), And then convert int to Class A object (Class type conversion).
Code aboveConvert 100 to the object of B, and then to the object of.SetError.
Suppress implicit conversion of conversion constructors:Explicit keywords
The single parameter constructor with the explicit keyword is not automatically executed (Explicit has no effect on the multi-parameter constructor.). During initialization, the explicit constructor can only be initialized directly.
The explicit keyword can only appear in the class internal constructor declaration.
15. static member variables and static member functions of the class
Static member of the classVariableRequiredThe class is defined and initialized once.Otherwise, it cannot be used correctly.