C ++ basics: Special descriptions of constant member functions

Source: Internet
Author: User

1. When passing a pointer, we can use the pointer to modify the content it points externally. However, it is impossible to modify the object pointed to by the external pointer. For example, if an external pointer is passed to the function to allocate space, the pointer or pointer reference must be passed.

2. char carry10 = 0; the compiler will set all the items after it;

3. When the return value of the function is const, the returned item cannot be left after it is indicated with the same type;

4. const int * I; int const * I; int * consti; the first two functions are the same, indicating that the content that I points to remains unchanged. The last one indicates that the address that the Pointer Points to remains unchanged, but the content is variable.

5. const member functions in the class. It is defined as adding const after the prototype. Constant functions cannot modify any attributes of a class. But there are two ways to modify it.

A) (myclass *) this-> mr1 = values;

B) define a member as mutable to be modified by the constant function.

6. constants of the const type in the class cannot be used to define arrays in the class. While enumONE = 100; TWO = 2; the defined ONE and TWO are acceptable. Generally, the allocation problem defined by enum is: enum a l = 9, Z; at this time, the value of Z is 10.

7. The int defined by const can be used to open up arrays. However, elements in the constant array defined by const cannot be used to define arrays.

8. Use sizeof to calculate the space of the variable. If it is an array, return by actual space; constant string (actually a variable opened in the static memory area) sizeof returns a value greater than the actual length. If it is a pointer, only the size of the pointer type is returned regardless of the space it points. If sizeof is used to calculate the row parameter of the function, even the Group returns only the size of a pointer of the relevant type.

9. For example, int iarray = 12,124,433; the compiler automatically assigns the length of three elements to the iarray. The formula for calculating the number of element lengths is sizeof (iarray)/sizeof (* iarray ).

10. copy constructor: When the row parameter and the real parameter are combined, if it is a value transfer type of a complex object, call the copy constructor to generate a temporary object as the real parameter and exit the function, the temporary object is released by calling the destructor. When the returned value is a complex object, it also calls the copy constructor to assign values. In this case, the number of calls of constructor and destructor is not equal. The prototype of the copy constructor is A (A &), which can be reloaded in the class. (The default copy constructor uses the bit COPY method: the shallow COPY method does not copy the content pointed to by the pointer ).

11. volatile variables tell the compiler that code optimization is not required for this variable. In multi-threaded applications, if we read a variable into the register, the time slice will expire and process other threads. When the processor is re-acquired, the volatile type will tell the processor, instead of directly processing the register data, you can re-read the data from the variable to the Register to prevent dirty data.

12. The class and struct have the same functions to a certain extent, except that the default members of the former are private, while those of the latter have a common lack of time-saving members. Therefore, class is not a required reserved word for c ++.

13. the c and c ++ compilers generate different identifiers for the same function name after compilation. Therefore, when referencing the c library file, you must use the extern "C" to tell the compiler, it is a c function that is compiled according to the c Rules. The standard header file we use has been processed.

14. # include "filename"; # include: the former searches for files in the current directory first. If it cannot be found, it can be found in the path specified by the system, the latter can be found directly under the path specified by the system.

15. static variables allocated anywhere have the same lifecycle as the primary process. The second definition of an existing static variable has no effect on the use of the variable, but its visible range is only within the defined range. (The postgraduate entrance exam made a mistake !) (It is not difficult to understand the characteristics of static variables. The static type in the class is shared by all objects)

16. The implementation of inline functions is similar to that of macros. When an inline function appears, expand the function to avoid the function calling from the stack, such as the stack, and improve the efficiency. But the cost of inline functions is: The Code increases. Inline functions are suitable for member functions and free functions. The functions implemented in the class are automatically inline functions. Inline must be defined on The Implementation of The function. For example, inlineintPlusOne (int) is invalid. A friend function is automatically implemented as an inline function in the class body.

17. # include

# Define DEBUG (X) cout <# X = <


# X indicates that X is output as a string.

18. assert (0! = 0); if the condition in assert is false, the program is exited during running and the row number of the error code is reported. (# Include)

19. The static object calls its own destructor only when main ends or exit () is called. This means that it is very dangerous to call exit () in the destructor of an object and may enter an endless loop. Call abort () to exit the function. The static object destructor will not be called. We can use atexit () to specify the operations to be performed when jumping out of main or calling exit. Functions registered with atexit can be called before the destructor of all objects.

Void exit_fn2 (void)

Printf (Exit function #2 called \ n );

// Processing functions

Atexit (exit_fn2 );

20. global variables actually use static storage. The construction of static variables is called before entering main, and its destructor is called at the end of main. Variable names are defined in a small scope (c ++ ):

// *. Cpp

Int a; // static variable, but extern int a; that is, it is global and externally visible

Static int B; // static variable. The opposite is static and extern, which is only valid in *. cpp and invisible to other units (files. The function definition is the same as above.

Main ()

The static member variables of the class can be assigned the following values: int X: s = 23; (in *. cpp, both public and private can be used)

21. namespace: Define a namespace, and then use unsing to convert the current type context to the location specified by the namespace.

Namespace math

Enum signpositive, negative;

Class integer

Int I;

Sign s;

Public:

Interger (int I = 0): I (I ).........

Sign Sign ().........

.......................

; // End class

Interger A, B, C;

Interger divide (interger, interger );

// No;

Void q ()

Using namespace math;

Interger A; // hides math:

A. Sign (negative );

Math: A. Sign (positive );

22. Generally, for the flaot f (int a, intb) function, some c ++ compilers generate the name of _ f_int_int after compilation, and some c compilers generate the name of _ f. Therefore, when linking c library functions in c ++, use extern "C" to tell the compiler to compile functions according to the c Rules. Similar to extern "C" # include "myhead. h", c ++ also supports extern "C ++ ".

23. When calling a function, passing a reference also forces the pointer to the stack.

24. The order in which constructor, destructor, value assignment constructor, and overloaded = are called: (all three functions have been implemented)

A) X x; X a = x;

Result:

X: construct

X: copy_struct

B) X x; X a; a = x;

Result:

X: construct

X: construct

X: copy_stru

Operator =

X: destruct

If no constructor is assigned, the result is:

X: construct

X: construct

Operator =

X: destruct

(If X a = x directly, do not use a General constructor to call the copy constructor)

Pointer to the member function of the class: Set int X: a (void)

X x;

Int (X: * pf) (void) = & X:;

(X. * pf )();

Pointer to the member variable: Set int I; to the member variable of X

Int X: * pm = & X: I;

X x;

Author: CSDN smallmark

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.