C + + Learning notes

Source: Internet
Author: User
Tags aliases throw exception volatile

Transferred from: http://blog.chinaunix.net/uid-20196318-id-2420689.html

Before learning C + + when the notes, because the recent development of C + + more, the notes turned out to review a bit, to share with you.

declaration and definition of a class
    1. class, struct, union reserved words can be used to declare and define classes. The Members in class default to the private type, struct, union, and C are compatible, and members default to the public type.
    2. C + + provides a default constructor/destructor only if the class does not explicitly define a construction/destructor, and the default constructor is only responsible for creating the object without doing any initialization work.
    3. When the program exits normally, destructors are implicitly invoked, and non-normal exits (such as abort) are not invoked, which may cause system resources not to be reclaimed in a timely manner. Constructors can only be called implicitly, and destructors may be called explicitly.
    4. Objects are automatically refactored when exiting their scope, and their objects are in the reverse order of their creation, and the constant object is immediately refactored after creation.
  access rights for class members

1. The object members of the private description are completely private, and even descendant objects of derived classes cannot access such members.

2. The object members of the protected description are private, protected, and descendants of the object's derivation can access such members.

3. The object members of the public description are fully exposed, and any object can access such members.

4. Any type of object member can be accessed by a friend function.

5. The access rights of a class can only prevent unintended unauthorized access, and by forcing type conversions, unrestricted access to members of the class can be made.

  Inline Functions

1. Any function member defined within the class will automatically become an inline function, regardless of whether an inline reserved word appears.

2. Defining an inline function outside of a class question must be explicitly described by inline.

3. The definition of an inline function must appear before the inline function is first called, otherwise it is called in normal form (inline failure).

New and delete

1. For simple types and classes that do not have constructors and destructors defined, Malloc/free and new/delete two memory allocation methods can be mixed, and if the class defines constructors and destructors, it is best to use new and delete to allocate and free memory.

2. When allocating space with new arrays, the first-dimensional subscript of an array can be dynamic, other dimensions require static, that is, an integer constant or constant expression, or, if the type of the array element is a class, and you want to create an array of objects with new, the corresponding class must define a parameterless constructor. If no constructors are defined, the parameterless constructor of C + + is used.

3. When you reload the new, delete operators in global space, the use of new in global space and delete will be overloaded versions.

4. When you overload the new, delete operator in class space, the overloaded version of the class is used for new and delete for the class.

5. Delete does not need to test whether the pointer is 0, because it is considered in the implementation of the Delete, there is no need to repeat the test.

implied by This pointer

1. The ordinary member function of a class is more than a static member function an implied parameter of the this pointer, which implies that the this pointer is the first parameter of a normal member function, and that the type of the parameter is a const pointer to such an object.

2. This can be used to distinguish a data member with the same name as the function member parameter.

3. This can be used to access the object that invokes the function member, the address of the object, and the reference to the object.

4. In non-const member functions, the Const declaration is as follows: C * const this;

In the const member function, the Const declaration is as follows: Const C * const this;

Initialization of Objects
    1. If a class defines a constructor, its object must be initialized with the constructor of the class definition, and the class must define constructors for those members when the class contains non-static data members for read-only and reference types.
    2. Assuming that the data member Class A contains non-static data members of Class B, if Class B defines a constructor with parameters, Class A must define its own constructor and cannot use the C + + default constructor.
    3. The constructor must initialize the object members of the object, read-only members and references, and can only be initialized once before the function body of the constructor, other data members can be initialized before the function body of the constructor, or they can be initialized again in the function body of the constructor function.
    4. Data members are initialized in the order in which they are defined in the class, regardless of the order in which they appear before the constructor body. If a simple type data member does not appear in front of the function body of the constructor, their value is initialized to 0 by default.
    5. When using a = a () at the time of definition, as with a (), only the constructor is called and operator= is not called.
name Space

1. The namespace reserved word is used to define namespaces. Namespaces must be defined within the global scope of the program and cannot be defined within a function, the name of the outermost namespace must be unique within the global scope of the program, and namespaces can be defined multiple times. Namespaces that do not have a name are called anonymous namespaces, and each program can have only one anonymous namespace.

2. The using reserved word is used to declare the namespace member that the program is referencing, or to indicate the namespace to which the program is referencing. If using A::X, X is introduced into the scope where the code resides, and x cannot be defined again, and if use namespace A is used, then x can still be defined, but must follow a::x to access the x in a.

3. Namespaces can be used to define aliases in place of long and obscure namespace names, such as namespace Abcd=a::b::c::D, you can access multiple namespaces directly with ABCD later.

const, volatile, mutable

1. A class that contains a const member must define a constructor that uses const to declare immutable data members, parameters and return values for function members, and so on.

2. A volatile-modified variable indicates that it may be concurrently accessed (modified), which tells the compiler not to make any access optimizations to the variable's access, that is, not using the register to hold intermediate calculations, directly accessing the object to get the latest value of the object.

3. The parameter table of a normal function member can appear after const or volatile, which is decorated with the object that the function member implicitly parameters the this pointer points to.

4. Mutable decorated data members are variable, and mutable members can always be updated, even in member functions of the const type.

  References

1. The alias of a variable at the time of reference, the referenced variable can be accessed directly by an alias, and the value of the pointer variable is the address of the variable, and the pointer is the value of the variable indirectly accessed through the address.

2. References must be initialized, and references to other variables (aliases) cannot be made after initialization, and reference parameters are initialized when the function is called.

3. References can be used to make functions of parameters or return values, avoiding the transfer of parameters when the large amount of data copy.

4. In the target code, references do not exist, where references need to be used, and objects that have been specifically referenced are substituted.

  referencing Objects

1. Reference variables are aliases to reference variables, which must be constructed and refactored on the referenced variables, and the reference variable itself is not necessary to construct and refactor.

2. A normal reference variable must be initialized with an lvalue, and if initialized with an rvalue expression, a temporary anonymous variable is generated, and the reference parameter is the same, such as a &q = new A (3), which must be delete &q after using the Q to release the space of the temporary anonymous variable.

3. The formal parameters of a non-reference type are local variables scoped to the function, and the destructor of the parameter object is done before the function call is returned, and the construction of the parameter object is passed by the value parameter at invocation, and the value parameter is passed to the data member of the parameter by assigning the value of the data member of the argument to the parameter. For pointer-type data members, only the value of the pointer is copied, not the storage unit that the pointer points to, which is a shallow copy. If a class contains pointer members, a shallow copy may cause memory errors, and a copy constructor must be defined to use the copy constructor for deep copies when the function is called.

  static data members

1. Static data members are used to describe the overall information of a class and must be defined and initialized outside the body of the class.

2. A static data member exists independently of a specific object, and its storage unit is not part of any object storage space, but the logical object shares this storage unit and cannot contain static data members when computing the storage space of an object or class.

3. Static data members describe the overall information of the class, because the global class functions with all program files, so the static data members of the global class must also act on all program files, i.e. int p::q = 0 when defined, not static intp::q = 0, but static when declared in a class.

4. The data members of the Union must share storage space, while static data members individually allocate storage units, so static data members cannot be members of the Union.

5. static data/function members can be accessed in three ways:

(1) A::member (2) A.a::member (3) A.member

static function members

1. The first parameter of the normal member function is the implied this pointer, and the static function member has no implied this parameter.

2. constructors, destructors, virtual functions and so on have this pointer, if the function member's parameter table after the const, volatile, then the function member's parameter table must contain the implied this pointer, these functions cannot be defined as static function members.

3. Union cannot define static data members, but you can define static function members.

from c turn to C + +
    1. Try to use const and inline instead of # define
    2. Try to use it without
    3. Try to use new and delete instead of malloc and free
Copy constructor call occasions

1. An object passed into the function body as a value

2. An object is returned from the function in the way that the value is passed

3. An object needs to be initialized with another object


function overloading and default parameters

1. C is a weakly typed language, and when a function is called, the type and number of arguments can be inconsistent with the function prototype, while C + + is a strongly typed language, and the number and type of arguments in the invocation must be identical to the function prototype.

2. A function with the same name is treated as an overloaded function, depending on the number of arguments or the type of the function.

3. You cannot define default parameters in both the function's prototype declaration and the function definition (declaring or defining a default parameter at any one location), and in the calling function, the non-default argument must pass the argument, and all default arguments must appear on the right side of the non-default parameter.

4. At compile time, by matching arguments and formal parameters to determine the overloaded function to invoke, when calling an overloaded function, if no matching function is found, or if more than one matching function (with default parameters) is located, the compiler will error.

operator Overloading

1. Overloading does not change the precedence and binding of operators, and in general, overloads do not change the number of operator operands (++/--need to differentiate between a predecessor or a post operator,-> change the number of operands).

2. Except for sizeof. *:: and three mesh operator?: All operators can be overloaded; operator =-() [] can only be overloaded to ordinary function members of a class, and cannot be overloaded with static function members or ordinary functions.

3. Operator overloading is for a single class object, not for a simple type or constant. If you overload an operator as a normal function, you must define at least one parameter that refers to a class or class, and the parameter cannot be a pointer to an object or an object array type.

4. If you overload the Lvalue operator (such as + =, =, and so on), the overloaded operator is best to return a reference type, and an operator that is overloaded with a normal function is usually set as a friend of the class for easy data access.

5. To differentiate between a single-purpose predecessor operator and a post-operator (+ + 、--), an additional int parameter is used as the identifier when overloading is a post-operator.

6. Operator overloading can be used to enforce type conversions, such as operator Int () const {} For a class, (const means that casting does not change the value of the current object), and the function is called when the class is converted to int.

static binding and dynamic binding  

1. Static bindings: Compile-time bindings, call through objects, dynamic bindings: Runtime bindings, implementation by address.

2. Static polymorphism: Function polymorphism--function overloading

Template polymorphism--c++ Template (class template, function template)

3. Dynamic polymorphism: Virtual functions (dynamic polymorphism can be achieved only with addresses)

4. Only virtual functions in a C + + class are invoked using the pointer-to-function () or reference variable. function () to perform dynamic binding. For non-virtual functions in C + +, because they do not have dynamic binding characteristics, dynamic binding is not performed regardless of the way the call is made.

Http://dev.firnow.com/course/3_program/c++/cppxl/2008313/104493.html

5. Dynamic binding is performed only through an address, that is, only through pointers or reference variables, and it must also be a virtual function. Conceptually, a virtual function mechanism works only when applied to an address, because the type information provided by the address during the compile phase is not complete.

Templates and inheritance

1. When the type of an object does not affect the behavior of a function in a class, a template is used to generate such a set of classes, such as a stack class that supports generics.

2. When the type of an object affects the behavior of a function in a class, it is necessary to use inheritance to obtain such a set of classes, such as a cat with different characteristics.

Virtual Functions

1. An overloaded function is a static polymorphic function, which is completed by static binding; Virtual function is a dynamic polymorphic function, which is accomplished by dynamic binding. Dynamic binding is very efficient, with only one pointer access more than a static binding.

2. Virtual functions are defined as virtual functions in the base class, and functions with the same derived class prototype will automatically become virtual functions, regardless of how many levels are derived, the attributes of the virtual function will persist.

3. A virtual function has an implied this pointer, so a virtual function cannot be defined as a static member function. The constructor has an implied this pointer, but the object type of the constructor must be deterministic and not be polymorphic, so the constructor cannot be defined as a virtual function.

4. For virtual functions of base classes and derived classes, their prototypes must be identical, but their access rights can be different.

5. Virtual functions can be defined as inline or overloaded with default and omitted parameters.

function Templates

1. By reserving a template to declare templates, the parameter table of the template in the declaration must be enclosed in angle brackets, each parameter must appear at least once in the function parameter table, and the parameters in the parameter table are not used in the implementation. When a template function is generated from a boilerplate function template, each parameter of the function template parameter table is replaced with a type.

2. For a function template with only one parameter, the system can be pushed automatically according to the parameter table, that is, the template special parameters can be omitted when called.

3. Templates can separate declarations and definitions, functions in templates can define default parameters, or they can be defined as inline functions.

4. The compiler becomes a template function based on the function instance generated by the function template.

5. Template functions can be generated explicitly based on a template, such as when a template function can be generated and called at the time of invocation.

6. You can implement an overlay function template with a specific type function.

Exception Handling

1. An exception is an event that unexpectedly destroys a program's normal process, and a program cannot detect an exception by detecting the value of a variable (this is called an assertion).

2. Exceptions can be caused either by hardware or by software. Hardware-thrown exceptions are typically generated by an interrupt service program, such as an arithmetic overflow and an exception generated by a divisor of 0; Exceptions thrown by software are usually generated by the throw statement, and both the operating system and the application can throw exceptions.

3. When the program throws an exception, the throw point establishes an object that describes the exception and throws the exception object, after which control is transferred (catch) to the exception's processing, and the object created at the throw point is used to initialize the parameters of the exception-handling process.

4. Statements in a try can throw multiple types of exceptions, and you can define multiple types of exception-handling procedures after a try. After you define more than one type of exception-handling procedure, the program executes one of the exception-handling procedures. After the corresponding exception-handling process is finished, the exception-handling process immediately followed is ignored, and the program continues to execute the statements after these exception-handling procedures.

5. After the exception handling process is complete, you can also execute a throw statement with no operands, continue propagating the exception of that type, the throw of which has no operand can only be executed in the catch (the throw with the operand can only be emitted in the try), regardless of the operand, all statements after throw are ignored , you know the exception handling process that encounters a parameter type match.

6. The exception-handling process must define a parameter that must be an omitted parameter (in ...). can match any exception) or a type-determined parameter.

7. Exception handling is matched by the order of catch blocks, the base class exception object can match the derived class object, and the catch block that omits the parameter matches any exception, so the catch block of the derived class is usually placed before the base class, and the missing parameter is dropped to the last.

8. Exceptions that are declared by the exception interface have exceptions that the function throws, which themselves do not want to catch and handle, exceptions that are defined by the exception interface appear after the function parameter table, and throw lists the type of exception to throw. Such as:

void Anyexception (); can throw any exception

void Noexception () throw (); Do not throw any exceptions

void noexpected () throw (A, b); Throw exception A, B

When declaring an exception interface, the function declaration is consistent with the defined exception declaration interface, and when the function declaration is thrown (a), a throw (a) may be specified when the function is defined.

C + + standard library

1. During the normalization process, C + + generates a new header file by simply removing the. h from the existing C + + header filenames, which is not important in itself, just as the resulting results are inconsistent. So it becomes, becomes, and so on. For C header files, use the same method, but add a C before each name. So C becomes, becomes, and so on. The last point is that the old C + + header file is officially objected to (that is, explicitly listed no longer supported), but the old C header file is not (to maintain compatibility with C). In fact, the compiler manufacturer does not stop supporting the customer's existing software, so it can be expected that the old C + + header files will still be supported in the coming years.

2. Old C + + header filenames will continue to be supported, although they are not in the official standard. The contents of these header files are not in the namespace Std.

3. The new C + + header file contains the same basic functionality as the corresponding old header file, but the contents of the header file are in the namespace Std. (in the process of normalization, the details of some parts of the library have been modified, so the old header files and the entities in the new header file do not necessarily correspond exactly.) )

4. The standard C header file continues to be supported. The contents of the header file are not in Std.

5. A new C + + header file with C library functionality has the name such as this. They provide the same content as the corresponding old C header file, except that the content is in Std.


deriving permission Control  

Private inherit from private

TD valign= "Top" width= "142" >

 

Protection inheritance protected

Public inherit publicly

Private

inaccessible

Not accessible

Not accessible

protected

Private

protected

protected

Public

Private

protected

Public

1. Public inheritance is referred to as type inheritance, and derived classes are subtypes of the base class.

2. Private inheritance is called implementation inheritance, and derived classes do not directly support the public interface of the base class, but instead, when it provides its own interface, it wants to reuse the implementation of the base class (the combination can do the same, depending on the situation).

3. Protection inheritance is primarily used for multiple levels of inheritance.

4. The common multi-inheritance pattern is to inherit the public interface of one class and the implementation of another class.

Construction order of derived classes

1. Call the constructor of the virtual base class, regardless of where the virtual base class appears on the inheritance hierarchy, before the non-virtual base class is constructed.

2. Call the constructor of the base class, in the order in which it appears in the inheritance list.

3. Call the data member's constructor or initialize the data member sequentially, in the order in which the data members are declared.

4. The body of the function that executes the derived class constructor.

5. The destructor sequence is the opposite of the construction process.

  C + + support for polymorphism

1. A pointer or reference from a derived class is converted to a pointer or reference to its common base class type through an implicit conversion.

2. Through the virtual function mechanism.

3. The conversion from a base class pointer to a pointer to a derived class is implemented by Dynamic_cast,typeid.

(go) C + + learning notes

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.