C + + Review notes __c++

Source: Internet
Author: User
Tags class definition exception handling inheritance

C + + is mainly referred to the "deep Exploration of C + + object Model" this book to review, this book to explain the C + + object model is very thorough, before also read the "C + + Primer" and "effective C + +", The last two books are also very well spoken. The following are mainly notes in the "Deep Exploration of C + + object model."

Member functions , although within the class declaration, does not appear in object, each non-inline member function will only be born with a function instance. As for each inline function with 0 or one definition, a function instance is generated on each of its users.

The main additional burden of C + + in layout and access time is caused by virtual , including:
1. virtual function mechanism , which is used to support an efficient "execution binding (runtime Binding)".
2. virtual base class , which is used to "base class that appears in the inheritance system multiple times, with a single and shared instance" .

C + + has two types of class datamembers: static and non static.
Three kinds of class member functions: Static,non-static,virtual.

For the polymorphic operation of object, this object must be accessed through a pointer or reference .

How much memory is required to be able to represent a class object, generally, mainly:
1. Total size of its non-static data members
2. The space that is allowed to be filled by the needs of the alignment may exist between members or at the boundary of the collection body.
3. Add any additional burdens that arise from the interior in order to support virtual

In 4 cases, the compiler must synthesize a default construct for class that is not declared construct:
1. When member object exists in class
2. Class exists inheritance system, base class's default construct
3. When there is a virtual function inside a class
4. When virtual base class appears in class inheritance system

There are 3 situations where an object is used as the initial value of another object:
1. Explicitly initializes an action with an object
2. When object is passed to a function as a parameter
3. Function returns object

A class object can be copied in two ways, one is initialized (copy construct) and the other is specified (Assignment) .

When does a class not present a "bitwise copy Semantics" ? There are 4 kinds of situations:
1. When class contains a member Objct, the latter class declaration has a copy constructor (either explicitly declared by the class Designer or synthesized by the compiler).
2. When class inherits from a base class , the latter has a copy constructor
3. When a class declares one or more virtual functions
4. When class derives from a chain of inherited strings, which has one or more virtual base classes

Two program expansion operations between compilers (as long as one class declares one or more virtual functions) :
1. Add a virtual function table (VTBL) that contains the address of each useful virtual function
2. A pointer to the virtual function table (vptr), placed in each class object

When class object is initialized with "another object of the same class", which is done in the so-called default memberwise initialization, the value of each built-in or derived data member, A copy of an object to another object, but it does not copy the member class object , but rather recursively execute memberwise initialization .

In the following cases, in order for the program to be compiled successfully, you must use a member initialization list : When initializing a reference member when initializing a const member when a constructor of a base class is invoked, and it has a set of parameters when it calls the constructor of a member class , and it has a set of arguments

The order in the member List is determined by the order of declarations in member

The compiler will process and possibly reorder initialization list one by one to reflect the order in which members are declared, and it will put some code into constructor and place any explicit user code before.
1. The additional data members that are automatically added by the compiler to support certain language features (mainly the various virtual features)
2. Because of the need for alignment (border adjustment)

Static member cannot do two points: Direct access non-static data is declared as const

The primary feature of the static member function is that there is no this pointer, a secondary attribute: it cannot directly access the non-static members of its class it cannot be declared as const, volatile or virtual. It does not need to be called through class object.

A single inherited virtual member function :
A class will only have a virtual table, each table containing the addresses of all active virtual functions function instances in its corresponding class object, these active virtual Functions includes: This class defines a function instance that overwrites (overriding) a possible instance of the base class virtual function function that inherits from the base class function instance, which is Derived class determines what happens when the virtual function is not overwritten

An instance of a per_virtual_called function that can either act as a space defender of pure virtual functions or as an exception handler for the execution period

When a class derives, there are 3 scenarios: it can inherit the function instance of virtual functions declared by base class, correctly, to copy the address of a function instance to the virtual of derived class Talbe of the corresponding slot it can use its own instance of the function, which means that its own instance address of the function must be placed in the corresponding slot it can join a new virtual function, this time virtual The size of the table increases by one slot, and the new function instance address is placed in the slot.

The

constructor may contain a large number of hidden code, because the compiler expands each constructor, depending on the inheritance system of class, in general the compiler does an expansion of approximately the following:
1. The data member initialization that is logged in the member initialization List is put into the constructor function ontology. And in order of the member's declaration order
2. If a member does not appear in member initialization list , but it has a default constructor, then the Strong>default constructor must be called
3. Before that, if the class object has virtual table pointer (s) , it must be set to an initial value, Point to the appropriate virtual Talbe (s)
4. Before that, all previous-level base class constructor must be invoked to base The order in which class is declared is in order (not related to the order in the member initialization list)

If base class is listed in the Member initialization list , then any display specified parameters should be passed in
If the base class is not listed in the Member initialization list , and it has the default constructor (or default memberwise copy constructor) , then call it.
If the base class is the second or subsequent base class of multiple inheritance, then the this pointer must be adjusted.

5. Before that, all virtual base class constructors must be invoked, from left to right, from the deepest to the lightest

If class is listed in the Member initialization list , then if any of the specified parameters are displayed, the past should be passed, if not listed in the list, and class has a default constructor, Also called the
In addition, the offset of each virtual base class subject in class must be accessible at run time
If class object is the lowest class, its constructor may be invoked; some mechanisms to support this behavior must be put in

Vptr Processing:

After the base class constructor invoke operation, but before the programmer-Supplied code or the members initialization action listed in the member initialization list .

the constructor execution algorithm is as follows :
1. In the derived class constructor , all virtual base classes and the constructor of the upper base class are called
2. After the completion of the above, the object's vptr will be initialized, pointing to the relevant virtual tables
3. If you have a member initialization list , you will expand in the constructor body, which must be done after vptr is set up to avoid having a virtual member function called
4. Finally, execute the code provided by the programmer

If the display refuses to assign a class object to a given class object
1. Declare it private (can only be invoked in member function and friend)
2. Do not provide definition (link will fail at call)

destructor Order of Expansion:
1. The function ontology of destructor is executed
2. If class has member class objects and the latter owns destructors, they are invoked in the reverse order of their declaration order
3. If object contains a vptr, then you need to reset the vptr and point to the appropriate base class virtual table
4. If any of the direct non-virtual base classes have destructor, they will be invoked in the reverse order of their declarations
5. If any of the virtual base classes have destructor, and the class previously discussed is the end of class, they are called in the reverse order of their original construction order

Conversion conversion

Three well-known extensions to the C + + language will affect C + + objects:
1. Template
2. Exception Handling (EH)
3. Runtime type identification (RTTI)

Class expression template will be evaluated at compile time rather than execution time, resulting in significant efficiency gains.

The member functions should not be "instantiated", and the C + + standard requires that they be instantiated only when member functions is in use, for two main reasons:
1. Space and time efficiency considerations
2. Mechanisms not yet implemented

Cannot assign an integer constant (except 0) to a pointer

The current compiler, in the face of a template declaration, only a limited error check can be performed before it is instantiated by a set of actual parameters, and any syntax-independent error in template, which the programmer may think is obvious, is passed by the compiler, and complains only when a particular instance is defined. This is a major problem in the implementation of technology at present.

In template, the result of a resolution of Non-member name is based on whether this name is used in relation to "the type of argument used to instantiate the template", if the use is unrelated, then "scope of the template Declaration "to determine name, if its use is related to each other, then the name is determined by the" scope of the template instantiation ".

A compiler must maintain two scope contents:
1. Scope of the template declaration: used to focus on the general template class
2. Scope of the template instantiation: used to focus on specific instances

The exception handing of C + + consists of 3 main vocabulary components:
1. A throw clause that emits a exception somewhere in the program, and the exception that is thrown out can be either an builtin type or a consumer custom type
2. One or more catch clauses, each catch clause is a exception handler, which is used to indicate that the clause is prepared to handle some type of exception and provides the actual handler in the enclosing curly braces section.
3. A try section, which is surrounded by a series of narrative sentences that may cause a catch clause to work

When a excpetion occurs, the compiling system must complete the following things:
1. Check for functions that occur throw operations
2. Determine if the throw operation occurs within the try Section
3. If, the compilation system must take exception type to compare with each catch clause
4. If the comparison is consistent, process control should be handed to the catch clause
5. If the occurrence of THORW is not in the try section, or if no catch clause matches, then the system must

(a) Destruction of all active local objects
(b) Remove the current function "unwind" from the stack
(c) Go to the next function in the stack of the program and repeat the steps above 2~5

Here are some notes in "effective C + +", "effective C + +" is mainly introduced in C + + programming in some traps and avoid methods.

Four programming paradigms in C + + (programming Paradigms):
1. procedure_based Process Oriented
2. objectb_based based on object
3. Object_oriented Object-Oriented programming
4. Generics Generic Programming

Note that the new Lambdal expression in C + + 11 is a support for programming on functions.

LHS for Left-hand-side: Left
RHS to Right-hand-size: Right end

If you do not declare it, the compiler declares a copy constructor for it (compiler version), a copy assignment operation that conforms to a destructor, and if you do not declare any constructors, the compiler will declare a default constructor for you, All of this function is public and inline.

The default constructors and destructors are primarily for compilers to place "hiding behind" code, such as constructors and destructors that invoke base class and non-static member variables. Note that the compiler produces a destructor that is non-virtual unless the base class of the class itself declares a virtual destructor (in which case the virtual property of the function virtualness, mainly from base class).

The copy constructor and the copy assignment operator, the version created by the compiler simply copies each non-static member variable of the source object to the target object (the static member variable is not copied).

Reference cannot change points to different objects.

You must customize the copy assignment if you intend to support assignment operators within a class that contains reference members or containing const members. This is because changes to reference or const members are illegal.

If a base class declares the copy assignment operator private, the compiler will refuse to generate a copy assignemnt operator for its derived class, after all, the copy generated by the compiler for derived class The assignment operator imagines base class components that can be processed, but they cannot invoke member functions that derived class cannot invoke.

Declaring member functions private and intentionally not implementing them can prevent copy assignment and copy constructs.
If the user attempts to copy the object, the compiler will report an exception.
If the member function or the friend function does so, the linker will report an exception.

public,protected,private Inheritance :

Public inheritance
From a semantic standpoint, public inheritance is an interface inheritance (it can be understood that the subclass object can invoke the parent class's interface, and it is possible to implement polymorphism).
From a grammatical point of view, after public inheritance, the relationship is shown above. It is obvious that the parent class public member is still public in the subclass, so the subclass object can invoke the interface of the parent class

Protected inheritance
From a semantic point of view, protected inheritance is an implementation of inheritance
Syntactically, after protected inheritance, the parent class public and protected members become protected members of subclasses, that is, the subclass object cannot invoke the interface of the parent class, only the function of the parent class can be implemented as a subclass, and of course it does not conform to the Liskov replacement principle (LSP) ".

Private inheritance
From a semantic point of view, private inheritance is an implementation of inheritance
From a grammatical point of view, after private inheritance, the parent class public and protected members become private to subclasses, which is more restrictive than the protected inheritance. It is said that the members of these parent classes can only be inherited once, and then continue to inherit, the members of the parent class will not be visible. Private inheritance is more incompatible with the Liskov replacement principle (LSP).

Richter Replacement (Liskov substitution PRINCIPLE,LSP): One of the basic principles of object-oriented design, the principle of the Richter substitution is that where any base class can appear, the subclass must be present, and LSP is the cornerstone of inheritance reuse. Only when the derived class can replace the base class, the function of the Software unit is not affected, the base class can be truly reused, and the derived class may add new behavior on the basis of the base class.

When you write a copying function (copy constructors or assignment operators) Make sure that you copy all local member variables, and call all the appropriate copying functions within the base clas.

The way objects are laid out and their addresses are calculated differently from the compiler.

inline is compiler behavior in most C + + programs, inline is just a request to the compiler, not a mandatory command. This application can be suggested at any time by metaphor, or it can be made clear that the metaphorical approach is to define the function within the class definition.

The compiler must know the size of the object during compilation.

The only thing that the C + + name hiding rule does is to obscure the name, and it doesn't matter whether the name should be the same or different type.

interface inheritance differs from implementation inheritance , under public inheritance, the derived class always inherits the base class interface.
The pure virtual function only specifies the interface inheritance.
The inpure virtual function specifies interface inheritance and default implementation inheritance.
The Non-virtual function specifically specifies interface inheritance and enforces inheritance.

In application domains (application domain), compounding means that has-a (there is one), in the implementation domain (Implementation domains), the compound means is implemented in terms of (implemented according to something).

Private inheritance is purely an implementation technology, meaning that implemented in terms of,private inheritance has no meaning in the software "design" level, and its meaning only extends to the software implementation level.

Private inheritance means implement in terms of, which is usually lower than the composite level, but is actually reasonable when inheriting classes need to access members of the protected base class, or if you need to redefine the inherited virtual functions. Unlike composite, private inheritance can cause empty base optimization, which may be important for library developers who are committed to minimizing object size.

When declaring template parameters, the prefix keyword class and typename are interchangeable.
Use the Keyword typename to mark the nested subordinate type name;
The base CALSS lists (base class list) or member initialization lists (the initial list of members) should not be used as the basis class modifier.

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.