C + + Third day

Source: Internet
Author: User

One. C + + four constructors:

1. Non-parametric construction:

2. The parameter structure: the default parameter can be used when the constructor

3. Copy Construction:

Class name
{
Class name (class name & that)
{
Assign a value to a class member
. ..
}
}

Student STU2 = stu1;//with stu1 to copy STU2

4. Assignment constructs:

void operator = (student& that)
{

}
Can interact with other objects
student& operator = (student& that)
{

}

Note: The compiler has a default copy and assignment constructor;

Two. Initialize the list
1. Is the initialization of a member, using parentheses to initialize a member of a class before the brace body of the constructor.
Class name
{
Class name (parameter list): member 1 (parameter) ...
{

}
}
1. Parameter list can solve the problem that the parameter of the constructor and the member name
2. The parameter list is executed before the constructor function
2. If the class member is an array, you can use {} to initialize the
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 that references a 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

Three. This pointer
1. Objects of the same type each have separate member instances, sharing member functions with each other
2. In order for the member function to know which object is being invoked and to have accurate access to the members of the object, the compiler automatically adds an invisible parameter to each member function
This parameter is a pointer to the calling object (this)
3. All member functions in the class have this pointer, including construction, hook, copy construction, etc.
Just construct this point to the object that is being created
4. is hidden by default (added automatically when member variables are accessed in member functions), but can also be displayed using the
5. Usage of this
1. Distinguishing member variables from parameters
2. Interacting with other members as the return value of the object

Four. Constant object and constant function
1. Constant object: Add the const keyword when creating an object, the object is not modified, there is a constant property
means that all variables in the entire object cannot be changed
2. The regular object cannot call the normal function, the call member function is equivalent to the object's this pointer to him, there is the risk of being modified
3. Regular objects can only call the constant function (the function body before the Const keyword called constant function), ordinary objects can also call the normal function
The constant function is equivalent to adding a const property to the this pointer
4. Constant functions and ' non ' functions can be overloaded, not conflicting
5. If a member does need to be modified, and it needs to be const-decorated, you can add a keyword mutable to the member, so that even if the constant object calls the constant function, the member can still be modified

Five. Static members and static member functions
1. Class members are static decorated and stored in the BSS segment (this segment is stored by the compiler and the size is fixed), in the program
When a dynamic object is created, its static members cannot be created, and all class objects share a static member
2. Static members can only be declared in a class cannot be defined in a class (must be defined outside the class)

Static type class Name:: statically member name;
3. A static member is a global variable declared in a class and can be used anywhere
Class Name:: Static member name for access

4. Static member functions: member functions in a class become static member functions after being modified by static
All members share a copy of a static member function
5. Static member functions do not pass the this pointer,
Static member functions can also be called without objects
Class Name:: Static member function (parameter)


Six. Single-case mode
1. Only one class of objects can be created, and this class is called a singleton class, which is called a singleton pattern.
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).

C + + Third day

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.