C ++ is required to take notes

Source: Internet
Author: User
Tags modifiers traits

C ++: Yes

Clause 1: Data abstraction

1. Select a descriptive name for the Type

2. List the actions that can be performed by the Type

3. Design interfaces for Types

4. Do not enable interfaces that affect the type.

 

Article 2: Polymorphism

Polymorphism type: class with virtual functions

Multi-State object: an object with more than one type

Polymorphism base class: a base class used to satisfy polymorphism objects

 

Clause 3: Design Model

1. the design model facilitates knowledge sharing.

2. All design patterns have their own unambiguous names

3. The mode must define the problems that this mode can solve.

4. Describe the solution to the problem

5. The mode description describes the consequences of applying the mode to a certain context.

 

Clause 4: STL (Standard Template Library)

1. Three major STL components: Containers, algorithms, and iterators

2. Excellent STL thinking: there is no need to know each other about the algorithms executed on containers. They are implemented through the iterator!

3. STL has eight containers. containers can generate a pair of iterators to specify an element sequence, while the algorithm operates on the sequence.

 

Clause 5: reference is not a pointer

1. Reference does not occupy memory space

2. All references must be initialized.

3. Reference always points to the object used for initialization

 

Clause 6: array parameters

Essentially, only a pointer pointing to its first element is passed in.

 

Clause 7: constant pointer and pointer to constant

Const T * pct = pt; pointer to const T. The object value cannot be changed, but can be pointed

T * const cpt = pt; a const pointer pointing to T. You cannot change the object to, but you can change the object value.

Pointer to constant T

Const T * 1st forms

T const * 2nd forms (easier to understand)

Constant pointer

T * const

A constant pointer can be converted to a constant pointer. The opposite is invalid!

 

Clause 8: multi-level pointer (pointer to pointer)

In class derivation, a pointer to a base class pointer is not a pointer to a subclass pointer!

A pointer pointing to a very large number of pointers cannot be converted to a pointer pointing to a constant.

 

Clause 9: New conversion Operators

Const_cast allows you to add or remove const or volatile modifiers of the expression type.

Note: 1, eye-catching, 2 less powerful, only affects Modifiers

Static_cast allows you to convert a pointer or reference of a base class in an inherited hierarchy to a pointer or reference of a derived class.

Note: although it is safer than the old-style conversion, it is not safe enough, and the runtime cost is low or not.

Reinterpret_cast allows you to treat an object as a bit, so that you can see one thing as another.

Note: It is used for underlying encoding, so proceed with caution.

Dynamic_cast is used to safely convert a pointer to the base class to a pointer to a derived class (used for Down conversion of polymorphism types)

Note: The runtime cost is higher than static_cast!

 

Clause 10: constant member functions

Member data cannot be changed in constant member functions! If necessary, declare the member data as mutable.

 

 

Clause 11: The Compiler places things in the class

If multiple virtual functions are declared in the class, the compiler inserts a pointer to the virtual function table for the objects in the class!

According to the regulations, some pointers will be placed at the beginning of the object or at the end of the object. If there is multiple inheritance, it may be in the object.

 

Clause 12: Assignment and initialization are different.

Assignment occurs when you assign values. In addition, all other replication conditions are initialization, including declaration, function return, parameter passing, and exception capturing.

 

Clause 13: copy operation

Copying constructor and copying assignment are two different operations, which usually appear in pairs.

 

Clause 14: function pointer

No function pointer pointing to any function

The address of a non-static member function is not a pointer. You cannot direct a function pointer to a non-static member function.

Calling an inline function through a function pointer will not result in an inline function!

 

Clause 15: pointers to class members are not pointers.

Int * ip address; pointer to int

Int C: * pimC points to a pointer to an int member of C.

A pointer to a member does not point to a specific memory location. It is an offset in the class. to access this member, you must add the object address and the member offset.

There is an implicit conversion from a pointer to a base class member to a pointer to a member of a public derived class!

There is no pointer conversion from a pointer to a derived class member to any of its base class members!

 

Clause 16: the pointer to a member function is not a pointer.

When obtaining the address of a non-static member function, we get a pointer to the member function instead of an address!

A pointer to a member function can point to a constant member function!

For a pointer to a member function, the object address must be used as the value of this pointer for function calling.

There is an implicit conversion from the pointer to the base class member function to the pointer to the member function of the public derived class!

 

Clause 17: processing functions and array declarations

Int * f1 (): a function that returns int *

Int (* f1) () A function pointer

Int (** ap2) [N]; A pointer pointing to a pointer, which points to an array with N int Elements

Int * (* ap2) [N]; a pointer pointing to an array with N int * Elements

Int (** const fp2) () = 0; a constant pointer pointing to a pointer to a function!

Int * (* ap2) () A pointer pointing to a function that returns int!

Int (* ap [N]) () an array of function pointers

For such complex statements, use typedef!

 

Clause 18: Function objects

A function object is also a common class object. It reloads the function call operator () and can be integrated through a VM!

 

Clause 19: Command mode and haole wuwu Law

Callback: Use the function pointer to respond to events!

Haole no-dock rule: Don't CALL us, we will CALL you

 

Article 20: STL function objects

You can create function objects that inherit from STL.

 

Article 21: heavy load and rewriting

The overload occurs when two or more functions in the same scope have the same name but different signatures.

When the rewriting class has the same name and signature as the derived class and the base class

 

Clause 22: Template Mode

It is a method in which the base class designer provides clear instructions for the designer of the derived class.

The template mode provides a base-class designer with an intermediate control mechanism, this control mechanism is between the "take it or leave it" mechanism provided by non-virtual functions and the "replace everything if you don't like it" mechanism provided by virtual functions.

The template mode is implemented as a public non-virtual function, which calls the protected virtual function. the sound dispatch class must accept all the implementations specified by its inherited non-virtual functions. You can also rewrite the protected virtual functions called by this public function!

 

Clause 23: namespace

Using command import name from namespace

 

Clause 24: function search

Call the function in three steps: Find the function name, match the best function, and check whether the function has the permission to access it.

 

Clause 25: Search for dependent parameters (ADL)

ADL is used to check the name of a function in a function call expression. The Compiler also checks the name space that contains the type of the function call arguments!

 

Clause 26: search operator functions

There are two call methods for operator function operations:

1. Non-member operator % call

2. Call member functions

For calls in 1st, the compiler not only considers member operators, but also non-member operators!

 

Article 27: capability Query

Dynamic_cast conversion through the "irrelevant" type is called horizontal conversion!

 

Clause 28: Comparison of pointers

If the implicit conversion from the pointer of the derived class to the base class pointer exists, the = comparison can be performed and the result is TRUE, because the multi-inheritance derived class object has multiple addresses

 

Article 29: Virtual constructor and Prototype mode

A member function that provides the ability to clone objects. It is implemented by indirectly calling constructor of its class through a virtual function. The effect is also a virtual constructor! This technology is called Prototype mode! With this function, you can generate copies of any type of base classes.

 

Clause 30: factory Model

Essentially, the base class provides a virtual function for producing appropriate products. Each derived Ray can override the virtual function to generate appropriate products!

 

Clause 31: return type of the covariant

If there is a virtual function in the base class that returns the base class type, when the derived class overrides this virtual function, the type of the derived class can be returned! This is legal

 

Cla32: copying prohibited

Prohibit the copy operation by declaring its copy constructor and copy operator as private.

 

Cla33: Create abstract base class

Method 1: Display and declare a constructor and copy constructor, and then declare to be protected, in order to allow the constructor of the derived class to use them, and prevent independent object Creation

Method 2. Declare the Destructor as pure virtual

Method 3. When a class does not have virtual functions and you do not need to declare constructors, a protected, non-virtual destructor is used!

 

Cla34: Prohibit and force use of heap allocation

One of the ways to specify that the object should not be allocated to the stack is to overload the operators new, new [] and define them as illegal!

Heap allocation is encouraged. You only need to declare the Destructor as private.

 

Clause 35: placenment new (Positioning new)

Placenment new allows us to "place" objects in a specific position to call a constructor!

Placenment new cannot use delete for it!

Instead, directly call the object's destructor!

 

Cla36: Class-specific memory management

Operator new and operator delete can be declared as member functions to change class memory management! If operator new and operator delete members are defined in the base class, make sure that the base class destructor are virtual.

 

Article 37: array allocation

T * p = new T; non-array form

T * p = new T [12]; array form

As long as a non-Array Function is declared, the array form should be declared for these functions.

 

Clause 38: Unusual Security kilometers

1. Exceptions are synchronized.

2. Destruction of objects is exceptionally secure

3. The exchange operation will not throw an exception

 

Clause 38: abnormal security functions

 

Clause 39: RALL

RALL indicates that resources are initialized when they are obtained. It uses the concept of the lifecycle of the C ++ object to control program resources!

The simplest form of RALL: Create an object and its constructor obtains a resource, while the destructor releases this resource!

 

Clause 40: new constructor and exception

If operator new is a member, it is best to declare operatordelete.

 

Cla41: smart pointer

It is a class type, but provides additional capabilities that cannot be provided by built-in pointers. It provides capabilities through constructors, destructor, and replication operators, to control and track what it points! The-> and * operators are also overloaded.

 

Cla43: Unusual auto_ptr

Auto_ptr is a class template used to generate specific smart pointers.

Benefits:

1: efficient

2: Automatic Recovery

3: In type conversion, it looks like a built-in pointer.

Unlike normal smart pointers, the replication operation! In general, the copy operation will not affect the source value involved in the copy! While auto_ptr is not a real copy operation, it actually transfers the control of the underlying object from one auto_ptr to another auto_ptr, and can regard the right parameter of the assignment or initialization operation as "Source ", the left parameter is treated as "receiver ". the control of the underlying object is passed from "Source" to "receiver ".

Avoid using auto_ptr in two places

1. It should not be used as a container Element

2. An auto_ptr should point to a single element instead of an array.

 

Cla44: pointer Arithmetic

Pointer arithmetic is performed based on the object size ratio.

Two pointers of the same type can be used for subtraction. The result is the number of elements between the two pointers involved in the operation. If the number of 1st pointers is greater than 2nd, the result is positive. Otherwise, the result is negative, if the two pointers point to the same object, the value is 0.

Two pointers have no addition, multiplication, and division.

 

Clause 45: Template terminology

Template <template parameter list>

Class <Template Name>

Template <template parameter list>

Class Heap <T *> template ID

Template <> class Heap <char *> List of real parameters in the template

Template parameters are used for template declaration.

The real parameters of the template are used to make the template special.

The template name is just a simple identifier

Template id refers to the Template Name with the template real parameter list attached.

Template specialization refers to what you get when you give a template with a set of template parameters. It can be displayed or implicitly performed.

Instantiation refers

 

Cla46: Special template display

Show that TDE is not a template

C ++ does not require the display of special interfaces to be exactly the same as the main template interface!

 

Article 47: localized template features

Function templates cannot be localized.

A class template can be localized. It is a completely unique template and a class template.

The localized syntax is similar to the fully-specific syntax, but its template parameter list is not empty.

 

Clause 48: Special template members

In addition to member functions, other members of the class template can be displayed with special features, including Jingtai members and Member templates.

Display special is a medium means to provide custom versions for templates or template members.

The display instantiation tells the compiler to instantiate a member.

 

Cla49: Use typename to eliminate ambiguity

You can use the typename keyword to tell the compiler that the qualified name is a type name and the compiler is allowed to parse the template correctly!

 

Clause 50: Member Template

 

Cla51: use template to eliminate ambiguity

Typdef typename A: template rebind <Node >:: otherNodeAlloc;

The keyword template equals to telling the compiler that rebind is a template name, while the use of typename tells that the entire expression 10 represents a type name!

 

Cla52: Special targeting type information

Template <typename X>

Struct IsInt

{Enum {result = false };}

Template <> struct IsInt <Int>

{Enum {result = true };}

The template instantiation mechanism is used to execute some calculations during compilation rather than runtime! (If it is an Int, it is an Int)

 

Article 53: embedded type information

To know the type of elements in a container, the most common method is to provide personal information by type!

Template <class T>

Class Seq {

Public:

Typedef T Elem; // element type

Typedef T Temp; // temporary object type

Size_t size () const;

 

Typedef Seq <std: string> strings;

String: Elem aString; query type!

 

Clause 54: traits

The traits class is a set of information about a certain type. Unlike the nested container information, it is independent of the type described by it.

Common applications: place an intermediate layer that complies with conventions between generic algorithms and types that do not comply with the expected conventions of algorithms!

 

Article 55: template parameters!

Whether or not to assign a template parameter name in the template declaration is optional.

The template parameters can have default values.

A template can contain a parameter that is a template name!

Template <typename T, template <typename> classCont>

Class Stack;

 

Clause 56: policy

Policy is a class or a class template. Its members are used to describe the configurable behavior of a generic component. It is usually used as a real parameter of the template!

 

Article 57: Derivation of template parameters

Let the compiler deduce the real parameters of the template based on the real parameters in the function call.

The compiler only checks the real parameter type of the function during the real parameter derivation of the template, and does not check the possible return types! Therefore, to let the compiler know the return type, except explicitly telling it, there is no other way!

Int a = cast <int> (12.3 );

 

Clause 58: overload function Template

You can design helper functions to execute the real-parameter derivation and specialization of templates.

 

Cla59: sfinae (replacement failure is not an error)

 

Clause 61: instantiate only things to use

It is not necessary to make all member functions of a class template be instantiated legally.

 

Clause 62: includes a record

# Ifndef XXX_H

# Define XXX_H

# Endif

 

 

# Ifndef HDR1_H

# Include "hdr1.h"

# Endif

 

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.