Some basic skills of C ++, including some special instructions on constant member functions

xiaoxiao2021-03-06  38

1. When the pointer is passed, we can modify the content pointed to by the pointer. However, if you want to modify the object pointing to the external pointer is not possible. For example, the external pointer to the function is transmitted to allocate space, and the pointer to the pointer or pointer must be passed.

2. CHAR Carry [10] = {0}; the compiler will set all the things thereafter.

3. When the function return value is const, the returned thing is paid to a type identical indicator; it cannot be left value;

4. Const Int * i; int * const * i; int * const i; the first two functions are the same, indicating that the content points to the i is unchanged; the last description pointer points to the point indicated, but the content varies.

5. Const member functions in the class. Defined as a CONST after prototype. Constant functions cannot modify any properties in the class. But there are two ways to modify.

a) {(myclass *) this-> member1 = values;}

b) Define a member to Mutable can be modified by a constant function.

6. The constant const type in the class cannot be used in the class to define an array. ENUM {One = 100; TWO = 2}; defined one, TWO can be. Usually an ENUM defined setup problem: enum a {l = 9, z}; at this time, the value of Z is 10.

7. Int defined with const can be used to open an array, but the elements in the constant array definitions cannot be used to define an array.

8. Calculate the space of the variable with the sizeOf, if it is an array, press the actual space to return; constant strings (actually the variable opened in a static memory area) SIZEOF returns to the actual length plus one. If it is a pointer, it is not considered to point to the space size, and only returns the size of the pointer type. If you use the SizeOf to calculate the row ginseng, even if the group is also returns a size of a related type pointer.

9. Shaped like int aRray [] = {12, 124, 433}; the compiler will automatically assign the length of the three elements to IARRAY. The number of element lengths is calculated as a sizeof (IARRAY) / SIZEOF (* IARRAY).

10. Copy Construction Function: When the collections and the actual parameters are combined, if the transmitted value of the complex object is called, the calling constructor generates a temporary object as an argument, and the temporary object is sent to the destructor is released. When the return value is a complex object, it is also a call copy constructor to assign a value. This will appear in cases where the constructor and the destructive function are not equal. The prototype of the copy constructor is A (A &), we can overload in the class. (The default copy constructor is a bit (bit) copy method: shallow copy, no copy of the pointer points to the content).

11. Volithile type variable tells the compiler, this variable does not need to optimize code optimization. In multi-threaded applications, if we read a variable to the register, at this time, the time film expires, dealing with other threads, when re-obtaining the process, the Volatile type tells the process, and read data from the variable to the register. Instead of direct processing with register data, it can prevent dirty data.

12. Class and struct have the same function to some extent, but only the former default members are private, the latter is common in the default member. Therefore, the Class is not a reserved word necessary for C .

13. C and C compiler, different indications that are generated after compiling the same function name, so it is necessary to use Extern "C" to tell the compiler when referenced to the library files referenced. It is a function of c, press C. Compilation. Usually we use the standard header files have been processed.

14. #include "filename"; #include , the former is first looking for files in the current directory. If you can't find it to find it to the system specified, the latter is directly to the path specified by the system. 15. The static variables allocated anywhere, the lifecycle and the main process is the same. The second time defines an existing STATIC variable, which has no effect on the variable, but its visible range is only within the defined range. (The postgraduate studies have made a mistake!) (It is not difficult to understand from the characteristics of static variables, and the Static type in the class is shared by all objects)

16. Inline function (inline) is similar to the actual and macro, where the function expands the function to avoid the function call, such as stack, and improve efficiency. But the price of the inline function is: the code is increased. The inline function is suitable for member functions and free functions. The function implemented in the class is automatically inlined function. Inline must be defined to the implementation of functions, for example: Inline int plusone (int) is invalid. The friend function is automatically changed to the inline function in the body of the class.

17. #include

#define debug (x) cout << # x "=" << x << Endl

The #x represents X is output as a string.

18. Assert (0! = 0); if the conditions in Assert are false, return the program during the run, and report the line number of the error code. (#Include )

19. The static object calls itself when the main or exit () is called. This means that in order to call exit () in an object's destructor, it is possible to enter a dead cycle. Calling Abort () to exit the function, the destructor of the static object is not called. We can use ATEXIT () to specify the operation you want to perform when you jump out of the MAIN or call EXIT, and use the ATEXIT registration function to call the destructor's destructor.

Void exit_fn2 (void)

{

Printf ("EXIT FUNCTION # 2 Called / N");

} // handler

Atexit (exit_fn2);

20. The global variable is actually static storage. The structure of the static variable is called before entering Main, and it calls its destructor. The name of the variable is made of small range (C ):

//*.cpp

INT A; // static variable, but for extern int a; 即 即 is global, external visible

Static int b; // static variables, STATIC and Ex Ex Ex Expern, only in * .cpp, it is invisible to other units (files). The definition of the function is the same.

Main ()

{}

The static member variable of the class can be assigned the following: INT x :: S = 23; (in * .cpp, no matter how private privacy)

21. Namespace: Define a namespace and then use unsing to convert the current type context to the name space.

Namespace Math

{

ENUM SIGN {Positive, 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

A.SIGN (Negative);

Math :: A.SIGN (POSTIVE);

}

22. Generally, for the function Flaot F (Int A, INT B); some C compiler compiles the name of _f_int_int, some C compiler generates the _f's name. Therefore, in the library function of the C , use externaln "c" to tell the compiler, press the rule to compile functions. Similar to extern "c" {# include "myhead.h"}, C also supports EXTERN "C " {}.

23. When the function is called, the pass reference is also a pointer stack.

24. Constructing function, destructuring function, assignment constructor, overloaded =, four call sequences of four: (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 there is no assignment function, the result:

X: construct

X: construct

Operator =

X: destruct

(If you directly x a = x; this does not drop with the general constructor, call the copy constructor)

Pointer to the member function of the class: set int x :: A (void) {}

X x;

INT (x :: * pf) (void) = & x :: A;

(x. * pf) ();

Pointer to a member variable: set int i; is a member variable of X

INT x :: * PM = & x :: i;

X x;

转载请注明原文地址:https://www.9cbs.com/read-78113.html

New Post(0)