C + + notes (2017/2/9)

Source: Internet
Author: User

This pointer
    • The this pointer acts on the object that the member function is acting on.
    • A non-static member function can be used directly this to represent a pointer to the object that the function acts on.
    • Pointers cannot be used in static member functions this .
Static members static
    • Definition: A member with a keyword added before the definition static .
    • Ordinary member variables each object has its own copy, while the static member variable is shared for all objects.
    • A normal member function must be specific to an object, whereas a static member function does not specifically work on an object.
    • Static members do not need to be accessible through objects.
    • How to access static members:
      • Class Name:: member name.
      • The name of the object. Member name.
      • Pointer--member name.
      • Reference. Member name.
    • A static member variable is essentially a global variable, and a static member variable exists for the class, even if an object does not exist.
    • A static member function is essentially a global function.
    • Setting a static member The purpose of this mechanism is to write global variables and functions that are closely related to certain classes into the class, looking like a whole, easy to maintain and understand.
    • In a static member function, you cannot access a non-static member variable, or you cannot call a non-static member function.
Member objects and enclosing classes
    • Classes that have member objects are called enclosing classes
    • When the enclosing class object is built, the constructors for all object members are executed before the enclosing class is executed.
    • The order of constructor calls for object members and the order in which object members are described in the class are not related to the order in which they appear in the member initialization list.
    • When an object of the enclosing class dies, the destructor for the enclosing class is executed before the destructor for the member object is executed. The order and constructor are called in reverse order.
    • The enclosing class object, if initialized with the default copy constructor, is also initialized with the copy constructor, which contains the member objects.
Friend Yuan friends
    • Friend function: A friend function of a class can access the private members of the class.
    • Friend class: If A is a friend of B, then A's member function can access the private member of B.
    • Relationships between friend classes cannot be passed and cannot be inherited.
Constant member functions
    • You can add a keyword after the member function description of a class const , and that member function becomes a constant member function.
    • The value of a property cannot be changed inside a constant member function, nor can a const member function be called.
    • You should use a keyword when defining a constant member function and declaring a constant member function const .
    • If a member function does not call a non-const member function and does not modify the value of a member variable, it is best to write it as a constant member function.
    • Two functions, the name and the parameter table are the same, but one is const , one is not, the overload is counted.
    • mutableMember variables can be const modified in member functions.

C + + notes (2017/2/9)

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.