C + + Easy to confuse knowledge point collation

Source: Internet
Author: User
Tags function prototype

1///////////////////////////////////////////////////////////////////////

//constant Pointers: variables cannot be modified, pointers can be modifiedConst int*p;int Const*p;//pointer constants: Pointers are not modifiable, variables can be modifiedint*Constp;//before the pointer is dereferenced, make sure that it has been initialized to a certain/appropriate addressint*p;//The memory of the pointer P is assigned, but is not initialized, and the pointer points to the memory of the data that does not know//pointer to array: pointer to an arrayint*p =New int[5];inta[5] = {1,2,3,4,5};int(*p) [5] = &A;//array of pointers: The elements of an array are addresses (pointers)intb1,b2,b3,b4,b5;int*ptr[5] = {&B1, &b2, &b3, &B4, &B5};//pointer to pointer arrayint* (*P) [5] = &ptr;//function pointer: A function return type is a pointer to a typeint*f (x, y);//the pointer function points to the pointer variable of the function, which is essentially a pointer variableint(*f) (intx);/*declaring a function pointer*/F=func;/*assigns the first address of the Func function to the pointer F*///array of function pointers: Each element of an array is a function pointer that conforms to the return value and parameter flags specified by the function pointer arrayDouble(*f_attr[]) (Double,Double);//pointer to array of function pointersDouble(* (*F_ATTR) []) (Double,Double);

//2///////////////////////////////////////////////////////////////////////
Temporary variables/reference parameters and const
-For a function:
(1) If it receives a general reference parameter and is intended to modify the variable, C + + disables the creation of the temporary variable, because if the function that accepts the reference parameter is intended to modify the variable passed as a parameter, creating a temporary variable will prevent the intent from being implemented.
(2) If it receives a const reference, its intention is not to modify the use of only the variable, when one of the following two conditions is met, C + + will create a temporary variable for the variable:
1. The type of the argument is correct, but not the left value;
2. The argument type is incorrect, but can be converted to the correct type.

--Use a const reference whenever possible:
1. Use const to avoid programming errors that unintentionally modify data;
2. Use const to enable the function to handle const and non-const arguments, otherwise it will only accept non-const data;
3. Use a const reference to enable the function to correctly generate and use temporary variables;

--the reference is well suited for structures and classes, avoids the resource consumption caused by a large number of copies, and can also make the return value a reference, and it is important to avoid returning memory unit references that no longer exist when the function terminates.
--For return types, the normal (non-reference) return type is an rvalue and cannot be accessed through an address, whereas a reference return type is an lvalue, which can be modified and taken, and of course, the const reference return cannot be modified.
--The non-reference return value of the temporary variable and function is an rvalue, non-fetch address.

//3///////////////////////////////////////////////////////////////////////
overloading, overriding, and overriding differences for class member functions
A. Features that are overloaded with member functions:
(1) The same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.
B. Overwrite refers to a derived class function overriding a base class function, characterized by:
(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword.
C. " Hide refers to a function of a derived class that masks a base class function with the same name as the following rule:
(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).
(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay)

//4///////////////////////////////////////////////////////////////////////
5 ways to store variables
Storage description Persistence Scope How chaining is declared
-------- --------    -------- ------- -----------
Automatic automatic code blocks are not in the code block
Register automatic code block No in code block, use the keyword register
Static, non-linked static code block none in the code block, using the keyword static
Static, externally linked static files outside of any function
Static, internal linked static files are not inside any function, use the keyword static

//5///////////////////////////////////////////////////////////////////////
Remember: Declaring a class simply describes the form of an object and does not create an object. Therefore, there is no space for storing the value until the object is created.
Therefore, for the need to declare constants in the class, you can take an enumeration, and for static constant integer variables, you can also directly assign values when the class declaration;
Note: Declaring an enumeration in a class does not create a class data member. In other words, all objects do not contain enumerations.
At the same time, C++11 provides a new enumeration-scoped enumeration. Regular enumerations are automatically converted to integral types, but scope enumerations cannot be implicitly converted to integers.

//6///////////////////////////////////////////////////////////////////////
Automatic conversion of classes and coercion of type conversions
1. In C + +, only constructors that accept one parameter can act as a conversion function (if there are 2 arguments, the second parameter provides a default value, which is also used as a conversion function). You can turn off this feature by using explicit.
2. Conversion functions for C + + classes (converting class objects to underlying types):
(1) The conversion function must be a class method;
(2) The conversion function cannot specify the return type;
(3) The conversion function cannot have parameters;
For example, a function prototype converted to a TypeName type is as follows:
[Explicit] operator TypeName ()
Note: TypeName indicates the type to convert to, so you do not need to specify a return type. A conversion function is a class method that means that it needs to be called through a class object to tell the function what value to convert. Therefore, the function does not require arguments.
In c++11, you can declare a conversion operator to be explicit by explicit.

//7///////////////////////////////////////////////////////////////////////
String class-related functions:

//1. The static variables in the class declaration are typically defined in the CPP file, such as:intString::num_strings =0;//2. Constructor of the string class:string::string () {len=4; STR= NWChar[1]; str[0] =' /'; Num_strings++;}//3. destructor for the String class:string::~String () {--num_strings; Delete[] str;//4. Copy constructor for the string class:String::string (ConstString &St) {num_strings++; Len=St.len; STR=New Char[len+1]; std::strcpy (str, ST.STR);}//5. Assignment operators for the string classString & string::operator=(Const string&St) {    if( This= = &St)return* This; Delete[] str; Len=St.len; STR=New Char[len+1];    std::strcpy (str, ST.STR); return* This;}//6. C_ Style string assignment operatorString & string::operator=(Const Char*1) {    Delete[] str; Len=Std::strlen (s); STR=New Char[len+1];    std::strcpy (str, s); return* This; }

8///////////////////////////////////////////////////////////////////////
How Virtual functions work:
Typically, the compiler handles virtual functions by adding a hidden member to each object. A pointer to an array of function addresses is saved in the hidden member. This array is called the virtual function table (virtual functions table,vtbl). The virtual function table stores the address of the virtual function declared for the class object. For example, a base class object contains a pointer to the Address table of all virtual functions in the base class. The derived class object will contain a pointer to the stand-alone Address table. If a derived class provides a new definition of a virtual function, the virtual function table will save the address of the new If a derived class does not redefine a virtual function, the VTBL saves the address of the original version of the function. If a derived class defines a new virtual function, the address of the function is also added to VTBL. Note that no matter whether the virtual function contained in the class is one or 10, you only need to add 1 address members to the object. Only the size of the table is different.
Attention:
1. You should typically provide a virtual destructor for the base class, even if it does not require a destructor. Because if you define virtual functions, which means using polymorphism, you define a virtual destructor for the memory that the dynamic object occupies. Otherwise, When you use a base-class pointer to a derived class object for a destructor, you will simply release the underlying data for the base class object, and the derived class object is still not successfully freed.
2. A friend cannot be a destructor, because a friend is not a class member, and only a class member can be a virtual function.
3. When a pure virtual function is defined, it indicates that the class is an abstract base class. Abstract base classes are generally used as base classes for other classes and cannot be used to create objects.

9///////////////////////////////////////////////////////////////////////
Inheritance and dynamic memory allocation (assuming that the base class uses dynamic memory allocations)

//Base Class Using DMAclassbasedma{Private:        Char*label; intrating;  Public: BASEDMA (Const Char*l ="NULL",intR =0); BASEDMA (ConstBASEDMA &RS); Virtual~BASEDMA (); BASEDMA&operator=(ConstBASEDMA &RS); ....}

The declaration includes a constructor that uses new as a special method that is required: destructors, copy constructors, and overloaded assignment operators.
1. Derived classes do not use NEW: you do not need to define explicit destructors for derived classes, copy constructors, and assignment operators.
(1) because the derived class does not need to perform any special operations, the default destructor is appropriate;
(2) The default copy constructor performs member replication, which is inappropriate for dynamic memory allocation. But for a derived class that does not use new, it can satisfy the requirements. When you perform a copy of a class member or inherited class component, the derived class uses the copy constructor of the base class to copy the base class portion of the derived class object;
(3) The same is true for assignments.

2. Derived classes use NEW: Derived classes need to explicitly define destructors, copy constructors, and assignment operators.
(1) The derived class destructor automatically calls the destructor of the base class, so its own responsibility is to clean up the work performed by the derived class constructor.
(2) Copy constructor Similarly, call the corresponding copy constructor of the base class to copy the base class part, the derived class part can only be accessed and copied by the copy constructor of the derived class.
(3) The same is true for assignments.

/////////////////////////////////////////////////////////////////////////
Include and private inheritance comparisons:
1. Contains an object member that is explicitly named, whereas private inheritance provides a child object member without a name.
2. For constructors, contains assignment statements that will use member objects; inheritance uses the member initialization list syntax, which flags constructors using the class name instead of the member name.
3. The method is invoked using the object name when it is included, while private inheritance is used to invoke the method using the class name and scope resolution operator.
4. Access the base class object. The inclusion can be accessed directly by using the object name, and the private inheritance has no name, and the derived class object is converted to a base class object by forcing the type conversion.
5. With 4th, access to the base class's friend function, private inheritance can be called to the base class friend function by converting the derived class object into a base class object, matching the base class friend function parameter.
6. For programmers, it is easy to understand that we can explicitly define the image of the contained class in the containing class, and then use that pair to call the member function of the containing class, which makes the programmer seem very clear, but private inheritance is not so intuitive, it makes the relationship between classes more like, and in many cases, This kind of relationship is more like and complex, and programmers have to deal with many of the problems of inheritance, such as the problem of the name of the function of the derived class and the base class, that is, the question of two semantics, or inheriting some methods or variables that should not be inherited, such as a base class on a derived class. The method of the base class is then extended to all subsequent derived classes. Obviously, as a class outside of a bitwise inheritance hierarchy, there is no such problem with the containing class.
7. The containing class can also contain multiple pairs of images of the same class, but private inheritance cannot do so.
8. Private inheritance has special attributes that are not included, such as the derived class of private inheritance and the base class, which is inherited and inherited, and we know that the protected members of the base class are also protected members in the derived class in the case of a public inheritance, and we can access it as in the base class. In the case of a private inheritance, a protected member of a base class is a private member in a derived class, and we can set member functions in a derived class to access it, but contains and is contained without this layer of relationship, because the contained pairs are located outside the inheritance hierarchy, and the protected members are only open to derived classes. Therefore, the containing class cannot access the protected member of the pair through the contained pair of images.
9. Another advantage of private inheritance is that the derived class can redefine the virtual function of the base class, which can only be used in the class, including without this feature.

/////////////////////////////////////////////////////////////////////////
Type conversion operators
(1) dynamic_cast: conversion upward in the class's inheritance system (derived class-a base class), without allowing other transformations (null pointers);
(2) Const_cast: Remove the const qualifier;
(3) Static_cast: Same as the generic type conversion operator;
(4) Reinterpret_cast: For dangerous type conversions, you can also convert the pointer type to an integral type that is sufficient to store the pointer representation.

C + + Easy to confuse knowledge point collation

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.