Summary of C + + knowledge points (Ii.)

Source: Internet
Author: User

First, copy constructor
    • 1, is a special constructor, is to use an existing object to construct its homogeneous copy object, that is, object cloning
  class 类名  {   类名(类名& that)   {    对成员挨个赋值   }  
    • 2. The compiler will generate a copy constructor by default
      Compiling the generated copy constructor by default copies each member of the class in bytes
      If there is a Class B member in Class A, the copy construction of Class B is automatically called in the copy construct of Class A
    • 3, the programmer can customize the copy construction to replace the default copy construction
      A, copy construction can only have one, cannot overload
      b, once the programmer has customized the copy construction, the compiler will no longer generate
      C, in a custom copy can be encoded to achieve the replication of members
    • 4. In general, the compiler generates a copy construct that is fully sufficient to not easily customize the construction
    • 5. When to invoke the copy construct
      A, object and object assignment
      B. Using object and function to pass parameters
      C, use the object as the return value
Second, the initialization of the list
    • 1, is a method of initializing a member, using parentheses to initialize a member before the brace of the constructor
  class  {   类名(参数列表):成员1(参数),成员2(参数)....   {   }  }

A, parameter list can solve the problem that the parameter of the constructor and the member duplicate
b, the parameter list is first executed with the constructor function

    • 2. If the class member is an array, you can initialize it with {}
    • 3. If a member is a class, you can display the call constructor in the initialization list
    • 4. If a member has a const member and a reference member, the initialization list must be used
    • 5. The construction order of class members is independent of the initialization list, but is related to the order of member definitions
Third, this pointer
    • 1. Objects of the same type each have independent member instances, share a member function with each other, how the member function knows who is calling it
    • 2, in order for the member function to know which object is being called and to access the object's members accurately, the compiler automatically adds an invisible parameter to each member function, which is a pointer to the calling object (this)
    • 3. All member functions in the class have the this pointer, including construction, destruction, copy construction, etc., except that this point in the construct is the object being created.
    • 4, this pointer is hidden by default (added automatically when member variables are accessed in member functions), but can also be displayed using
    • 5, use the case use this
      A, distinguishing member variables and parameters
      B. Interact with other objects as return values
Four, constant object and constant function
    • 1, the creation of the object when the Const keyword, the object can not be modified, there is a constant attribute, it means that the whole object can not be modified all things
    • 2, the regular object cannot call the ordinary member function, the call member function is equivalent to the object's this pointer to it, there will be the risk of being modified
    • 3, the function body before adding a const key called constant function, the constant object can only call the constant function, ordinary objects can also call the constant function
      The constant function is equivalent to adding a const property to the this pointer
    • 4, the constant function and the ' non-constant ' function can form overloads that do not conflict
    • 5, if a member does need to modify, it is also modified by the const, you can add a keyword to the member mutable, so that even if the regular object calls the constant function can still be modified
Five, the destruction function
    • 1, when the object is destroyed automatically called the function called destructors, the entire life cycle of the object can only be called once, it is the object is destroyed before the last execution of the action
  class 类名  {   //不能重载,只能有一个   //不可以有返回值,没有参数   ~类名(void)   {   }  };
    • 2, the compiler will default to produce a destructor, the default destructor is responsible for destroying the members can be seen, if there are members of the class, will automatically call the member's destructor, class members of the destruction process and the construction process is reversed
    • 3. Destructors cannot be overloaded, but can be customized, with custom destructors default destructors do not generate
    • 4. When there are resources that the destructor cannot see in the class, when there is a need to restore the settings (close the Open File/save the acquired data), then you need to customize the destructor
Vi. Assignment Construction
    • 1. An assignment construct is a function called when an object assigns a value to another object
  Student stu2 = stu1; //拷贝构造  void func(Studnet stu); //拷贝构造  func(stu1;)  Student func(void) //拷贝构造  {   return *this;  }  Student stu = func();
    • 2. The format of the assignment constructor
  void operator = (Student& that)  {  }    //可以与其他对象进行交互  Student operator = (Student& that)  {  }
    • 3, the compiler will default to generate assignment constructs, its function and copy constructs the function, the object A is completely copied to object B
    • 4. Difference between assignment structure and copy construction
      Copy construction: Use Object A To create object B (called when object B has not yet been generated)
      Assignment constructs: Both object A and object B have been constructed, at which point B = A;
      If there are constant members in the object, the copy construct can be called successfully, but the assignment is not constructed.
    • 5, in general, the default assignment construction is basically enough, unless a member is a pointer, pointing to the additional memory space, in this case, the need to customize the copy construction, assignment construction
    • 6. Custom Assignment Constructs
      A, determine the format of the assignment construct
      b, prevent self-assigned value
      int num = 1;
      num = num;
      C. Releasing old resources
      D. Allocation of new resources
      E. Copy New content
      F, Code utilization (show call copy construction)
Vii. static members and static member functions
    • 1, when the class member is modified by static, it will be stored in the BSS segment (this segment is compiled and fixed size), the dynamic creation of objects in the program when its static members can not be created, all class objects share a static member
    • 2, static members can only be declared in the class cannot be defined in the class (must be defined outside the class)
      Type class Name:: member name;
    • 3. A static member is a global variable declared in a class, and you can use the class name in any location:: Static variable name
    • 4, the static member function, the member function in the class is modified by the static member function, all objects share a static member function
    • 5. Static member functions do not pass the this pointer and cannot access member variables
      Static member functions can also be called without objects
      Class Name:: Static member function (parameter)
Eight, single case mode
    • 1, can only create a class of an object, this class is called a singleton class, this pattern is called a singleton mode
    • 2, why need a singleton mode, is to improve security and stability of the skills.
      Only allowed instances of unique objects exist
      Commercial application of the singleton mode:
      Website counter
      Log Management System
      Connection pool, thread pool, memory pool
    • 3. Get the object instance's Special method
      A, the global variable definition is not controlled, can prevent the gentleman cannot prevent the villain
      B, the special method is part of the class, "I am the type I am the Boss",
      Classes prohibit the creation of objects externally, and only the interface that gets the objects is provided inside the class.
    • 4, how to implement a single case mode
      A, disallow creation of instances outside the class, private all constructors
      B, the class itself maintains its only instance,
      Static member variable static class name instance;
      Static member pointer the static class name * instance;
      C, provide access to the instance of the method, static member function getinstance ()

    • 5, a hungry man single case mode
      The objects are already created, regardless of whether they are needed.
      Advantages: High efficiency, fast speed, stable.
      Cons: Wasting resources, regardless of the need for objects have been created well;

    • 6, lazy single case mode
      Objects are actually created when you first use the Get object.
      Advantages: Saving Resources
      Cons: Low efficiency, slow speed, unsafe (multi-threaded case).

Nine, operator function

In C + +, the compiler has the ability to interpret an expression that consists of data, objects, and operators as a call to a global or member function. This global or member function is called an operator function, and by redefining the operator function, you can implement an algorithm for the custom type, and make it participate in the various expressions as the built-in type.

Ten, binocular operator expression
    • member functions
      Like the L#r binocular operator expression, interpreted by the compiler as l.operator# (R)
      A-b+c = = a.operator-(b). operator+ (c)
    • Global functions
      The l#r-shaped binocular operator expression is interpreted by the compiler as:: operator# (L,r)
      A-(b+c) = =:: operator-(a,::operator+ (b,c))
Xi.-Monocular operator expression
    • member functions
      A single-action expression, such as #o or o#, is interpreted by the compiler as o.operator# (), and the only operand is the calling object.
    • Global functions
      A single-action expression, such as #o or o#, that will be interpreted by the compiler as: operator# (O), the only operand is the calling object.
12. Typical Binocular operator overloading
 成员函数 Point operator /+-*%|^& (Point& that) {  Point t; // 会调用无参构造  t.x = x / that.x;  t.y = y / that.y;  return t; // 不能返回局部对象的引用,否则会出现悬空引用 } 注意:原对象的值不变,要产生一个临时的对象 bool operator > < >= <= == != || && (Point& that) { } Point& operator += -= *= /= (Point& that) {  return *this;  } 注意:运算符的重载要符合情理。

Global functions
A private member of the parameter may be accessed:
1, the members into public, but will undermine the closure of the class.
2, declare the global function as friend (friend is not a member),
3. You cannot access member variables directly in the friend function.

 Point operator + (Point& a,Point& b) {  Point t(a.x+b.x,a.y+b.y);  return t; }
13. Typical single-mesh operator overloading
 成员函数: 前++/--  Point& operator ++/-- (void) { } 后++/-- Point operator ++/-- (int) { } 全局函数: 前++/--  Point& operator ++/-- (Point&) { } 后++/-- Point operator ++/-- (Point&,int) { }
14, input, output operator overloading
 输入、输出运算符不能重载为成员函数,只能是友元。 ostream& operator << (ostream& os,Point& p) { } istream& operator >> (istream& is,Point& p) { }
XV, special operator overloads

[] subscript operator, you can use the object as an array.
() function operator, you can use the object as a function.
Member access operator, you can use the object as a pointer.

    • The dereference operator allows you to use the object as a pointer.
      New/delete can also be overloaded, but it is not recommended.
      New will automatically call the overloaded new function to the constructor.
      Delete calls the destructor and calls the overloaded delete function first.

      Overloading of only very individual operators is meaningful for objects (>>,<<)
      Frequently used operator overloads: Front ++/--, after ++/--

16. Some limitations of operator overloading
    • 1. Operators that cannot be overloaded
      :: Scope Qualifier
      . Member Access Operations
      . * Member Pointer dereference
      ?: three mesh operator
      sizeof byte length operator
      typeID Type information operator
    • 2. Overloading of operators cannot change precedence of operators
    • 3. Cannot change operator number of operands
    • 4. Unable to invent new operator
    • 5, overloaded operators should pay attention to the consistency of operators
      Do not change the operator's default operation rules
    • 6. Overloading of operators is a tool for easy use, enhanced readability, and should not be successful.


From for notes (Wiz)

Summary of C + + knowledge points (Ii.)

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.