C + + Primer Learning Summary 7th Chapter class

Source: Internet
Author: User

7th Chapter Category


1. Introducing the const member function (c + + Primer p231-232)

A const object of a C + + class cannot call a non-const member function, and if you want a const object to call a member function, you must declare it as a constant:

2. After the end of a class, the const member function returns *THIS, then its return type must be the const class name & The preceding const is not less. Otherwise, it cannot be compiled.

3. Default constructor p235-236

The compiler will synthesize a default constructor for us only if it discovers that the class does not contain any constructors, and the function performs a default initialization for members in the class ( if the class member has an initial value in the class, the default initialization is not performed ).

for built-in type members , the default initialization of local objects gives random values , and the global object Default initialization is given 0.

for Class-type members , default initialization calls the default constructor for the class .

The compiler may not be able to synthesize one class without constructors. If Class A does not have a default constructor, but a class contains object member B of Class B, and Class B does not have a default constructor, the compiler will not be able to initialize member B in Class A objects with the constructor of the composition. So at this point the Class A will not have the default constructor for the composition:

If the constructor of a class assigns only the initial values to some members, the remaining members will get a default value ( Initialize directly If there is an initial value in the class , otherwise perform the default initialization ).

4. The difference between copy and assignment P239

Objects are copied in several cases: initializing variables, passing or returning an object as a value.

When we use the assignment operator, an assignment of the object occurs.

5. Friend P241


Class A defines two friends, the function print () and Class B, so that private member X of Class A can be accessed inside the function print () or inside Class B.

Note: If you want to cout<<b.a.x<<endl; directly, you're wrong, because X is private, and even if Class A is a friend of B, you can't directly access A's private member. The meaning of a friend is simply that inside a class or function you can access private members of other classes.

A further exploration of friend Yuan :

Suppose there are Class A and Class B, and now want to define a Class A function print () as a friend in class B, how do you define the order of precedence?

You should follow the order above: Declare Class B before you define Class A, but the print function of Class A can only be declared and cannot be defined.

you can understand that . :

A class definition precedes the class B definition ( But at this point print () only declares undefined ): since Class B is going to use the member function print () of a Class A as a friend, it is obvious that class A is defined before class B, because then you can refer to the print () member function of Class A When you define Class B.

A class B declaration precedes the class A definition : Class B Second, because the print () member function has a Class B formal parameter, you need to declare Class B before you can define category A.

The class B definition precedes the print () function definition : Since the definition of the print () function is used in the members of Class B, you define the print () function, Class B must already be fully defined.

friend function Declaration :

Suppose Class A declares the friend function print () with friend. Print () can not be declared at this point, but no code of Class A can call print (). Class A can only call the print () function after the print () function is declared globally. This means that when the print () function can be used, it is only related to whether it is properly declared.

6. Variable data member (mutable keyword) P245 even if it is a const object, mutable members can be changed.

A is a constant object, and a can only call the const member function.

7. Suppose that Class A has a set method and a print () method, which we can use as follows:A.set. Set (+). Print (); when set () returns a reference to *this!

Because the dot operator is left-associative, the left is calculated first. The set returns a reference to *this, so you can continue calling other member functions.

Note: If the set () function is const, then the *this returned is a const object, then A.set () cannot invoke the non-const print () function. The p247-248 page is visible on this point.

8. Incomplete type : A class is declared only, but not yet defined. P250

You can define a pointer or reference to the type, or you can declare a function that takes an incomplete type as a parameter or a return value. However, you cannot define an object of an incomplete type. So:

A class can contain pointers or references to its own type

but cannot contain objects of its own type .

9. Type name scope: P255

The type name defined in the class can override the type name defined outside the class, provided that the type name outside the class is not used before the current definition statement:

Because the money definition was used when defining Val, it was wrong to define the new money type. If the new money type is on line 4th, then it is correct.

10. Constructors for some classes must be initialized: const members, references, or class types that do not provide a default constructor. Because these members are meaningless if not initialized.

The Class A constructors above do not initialize V2 and v3, so it is wrong. The constructor of a should initialize the initial list of values for V2 and v3. Instead of assigning values directly in the constructor ( assignment is not initialized ).

11. The initialization order of the members in the constructor is based on the order in which they are defined in the class, rather than in the order of the members in the initial list of values:

V1 is initialized first, so v1 will be a random value after V1 (V2). v2 after initialization, so V2 will be 500.

12. Delegate constructor usage (P261, tested by non- c++11 compilers also available)

Note: Assuming that constructor 1 delegates the constructor 2 constructs the object, then the constructor 1 even before the constructor 2 is defined, the 1 function can still call the 2 function,

13. The default constructor is automatically executed when the class object is initialized by default or when the value is initialized, so try to write a default constructor for each class, otherwise it is prone to error.

convert constructors , implicitly class-type conversions (P295)

If the constructor accepts only one parameter, it actually defines the implicit conversion mechanism that is converted to such a type. That is , We can use that parameter instead in the place where the class object is needed .

In Class A, the object of Class A is replaced with 100 and CIN respectively.

only one-step class type conversions are allowed ! However, built-in type conversions support multiple steps. That is, the above statement can be written as a1=a1+100.3; Turn 100.3 to Int ( built-in conversion ), and then convert int to Class A object ( class type conversion ).

The code above wants to turn the object into B , and then turn it into a object. An error will occur .

Suppressing implicit conversions of conversion constructors:explicit keyword

A single-argument constructor that uses the explicit keyword is not executed automatically (explicit has no effect on a multi-parameter constructor ). And when initialized, the constructor of explicit can only be initialized directly, Cannot assign a value to initialize.

The explicit keyword can only appear in the class internal constructor declaration.

15. Static member variables and static member functions for classes

A static member variable of a class must be defined and initialized one time outside of the class , otherwise it cannot be used correctly.

C + + Primer Learning Summary 7th Chapter class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.