Review
The following content contains personal insights
Ⅰ, sometimes have to use the constructor initializer list, because some compilers do not support the initial values in the class, which prevents initialization within the function body.
It is important to note that data members that are ignored by the initialization list are initialized by default (the result depends on the location and can have dire consequences). Here are the N cases of the constructor:
- The programmer directly ignores the constructor--the constructor that produces the default composition--the member is initialized by default--an indeterminate result.
- Provide the initial value to the data member (so-called property initialization in Java), do not write the constructor--the default composition constructor uses the provided initial value to initialize the corresponding member--the ignored member is initialized by default.
- The initial value in the class is not used, and the constructor initializer list-the ignored member is initialized by default. 、
- ·····
The situation is so dizzy, it is better to remember the better practice: provide the initial value in the class + display to initialize each member without the initial value in the class.
This is summed up by ensuring that each member has a definite value after creation, but not repeated initialization (repeated initialization: the initial value in the class is the same as the constructor assignment).
Ⅱ, copy, assignment, and destructor are also available in synthetic versions!
The compiler will do it for us if it is not written, but in some cases we cannot rely on the synthesized version:
That is -tube-reason-dynamic-state-inner-stored Class!!! (manual emphasis, except for the class containing string and vector)
Methodological summary:
- There is usually no need to write manually.
- When appropriate, use string and vector to dynamically allocate memory as much as possible !
Ⅲ, friend functions require a special declaration outside of the class. Example:
Class.h
#ifndef class_h_included #define Class_h_includedclassint getno (const person&); Private : int - ;}; int getno (const// necessary#endif
Class.cpp
" Class.h " int getno (const person& x) { return x.no;}
Main.cpp
" Class.h " <stdio.h>int Main () {person p; printf ("%d\n", Getno (p)); return 0 ;}
Operation Result:
[Email protected]:~/cprg# g++-std=c++ one main. CPP class. CPP [email protected]:~/cprg#./
Related reading:
C + + 34th day: Some content related to classes