- in C + + to use header files, the main reason is that C + + the same project may contain multiple source code files, these source code files are compiled separately, that is, when compiling one of the files, the compiler does not know the other files defined in the content, such as class global variables, etc. So we need to declare it in every file where we want to use a class, function or variable, otherwise C + + cannot find it
- The header file provides a centralized location for all extern object declarations, template class definitions, constants, function declarations, and inline function definitions and template function definitions, which means that symbolic constants and inline functions can be defined multiple times. The header file provides two security assurances:
- ensure that all files contain the same declaration of the same global object or function
- If you need to modify the declaration, you only need to modify a header file
- header files cannot contain definitions of non-inline functions or objects
- An inline function must be defined in every file that calls it, or it will cause compilation to fail. Functions defined in a class definition are inline functions by default, so a function above one or two rows is best defined outside the class body. Can be in a header file with the class body. A function defined outside the body of the default class is not an inline function if no inline decoration is used.
- to prevent a header file from being duplicated, you typically use the precompiled Directive # define in the header file, #ifndef, #endif来书写头文件内容
- data members cannot be initialized in the class body, except for static constant data members that are shaped. Shaping a static constant data member should be redefined outside the class even if it is initialized within the class, but cannot specify an initial value. If the class is not initialized, it is initialized outside of the class
- in general, static members should be initialized outside of the class body, As with global objects, static data members can only be defined once, so they should not be placed in the header file.
- A static member function cannot be declared as const or volatile and is declared in the class body to add the static keyword, which cannot be added when defined outside the class body
- performing two delete runs on the same area of memory is a mistake, typically assigning the pointer to null after the first delete, and then executing the delete without error.
- the first dimension of a dynamically allocated array does not have to be a constant, such as char *str = new Char[dimension];d imension is a variable
- A function pointer cannot be assigned to the address of a member function, even if the return type and the parameter table exactly match. Defining a class member function pointer requires a return type, arguments, and classes, and pointers to class member functions are always accessed through specific objects or pointers to objects of that type, such as:
int (screen:: *ptr) (int,int) =&screen::add; screen-screen; **ptrscreen=New Screen () (Ptrscreen->*int screen::* Ps_screen;ps_screen ptradd= &screen::add; Screen Myscreen;myscreen. *ptradd (x, y)
- the static member function pointer of a class is the same as a normal function pointer and does not require a class object when called
- :: is a domain operator that represents the global scope when used alone
- volatile prevents the compiler from optimizing the variables it modifies
- You cannot take function arguments when using a function that introduces namespaces. The function introduced by the using declaration is as if it were declared where the using declaration appears
- C + + 's empty class size is a byte that uses this byte to mark different objects because different object addresses are different
- overloaded functions must be in one domain
- The extern "C" link indicator can only specify a function in an overloaded set of functions, because the two overloaded functions compiled with extern "C" will have the same name, causing the program not to know which one to call
- when the parameter type is const or volatile, if the recognition function declaration is the same, regardless of the const or volatile, if const and volatile are applied to the parameters of the pointer or reference type, the const and volatile // The following two functions are duplicate declarations, not overloads
void f (int); void F (constint);
- The difference between new operator and operator NEW:
- new operator: is the operator that is used when defining a dynamic object, which is built into the language, cannot change meaning, is allocated memory, and calls the constructor to initialize an in-memory object. The function that the new operator allocates memory calls is operator new.
- operator NEW: The responsibility is to allocate memory, independent of the constructor, which can overload // Allocate memory
* A;
void *ptr = (A *)::operator new(sizeof(A));
A=new (PTR) A ();
C + + small knowledge points